[Tutor] skip some directories

Jonas Melian jonasmg at softhome.net
Tue May 31 09:04:25 CEST 2005


Danny Yoo wrote:
>>I'm trying to working with some directories only
>>
>>import os
>>dirName = "/usr/share/fonts/"
>>dirBase = ['misc','TTF','Type1','CID','100dpi','75dpi'] # directories to
>>skip
>>
>>for root, dirs, files in os.walk(dirName):
>>    for end in dirBase:
>>        if root.endswith(end):
>>            print 'skiped'
>>        else:
>>            print root
> 
> Let's break out the logic a little bit.  Would it be accurate to write
> this as:
> 
> ### Pseudocode ###
> for root, dirs, files in os.walk(dirName):
>     if weShouldSkipThis(root):
>         print "skipped"
>     else:
>         print root
> ######
> 
At the end, i found the way of make it:

dirBase = ['misc','TTF','Type1','CID','100dpi','75dpi','encodings', 'util']

for root, dirs, files in os.walk(dirName):
    print dirs  # for checking all directories that i have at beginning
    for name in dirs:
        if name in dirBase:
            dirs.remove(name)
    print dirs  # compare if all directories in dirBase has been deleted

But it fails at delete some directories. Why?

['100dpi', '75dpi', 'TTF', 'Type1', 'arphicfonts', 'baekmuk-fonts',
'corefonts', 'cyrillic', 'default', 'encodings', 'freefont',
'kochi-substitute', 'local', 'misc', 'sharefonts', 'ukr', 'util']

['75dpi', 'Type1', 'arphicfonts', 'baekmuk-fonts', 'corefonts',
'cyrillic', 'default', 'freefont', 'kochi-substitute', 'local',
'sharefonts', 'ukr']

It did fail with '75dpi', and 'Type1'


More information about the Tutor mailing list