[Tutor] for loop problems

alan.gauld@bt.com alan.gauld@bt.com
Mon Nov 4 12:21:25 2002


> directory = "/home/chris"
> 
> def checkDirectories():

better to pass directory as a parameter to the function 
- possibly with a default value - '.' say?

>     listDirectory = os.listdir(directory)
>     listDirectory = listDirectory[:]

Huh? This copies listDirectory to itself?

>     endingMark = '/'
>     
>     for element in listDirectory:
>         pathname = '%s/%s' % (directory, element)

I think you really want the glob module for this type 
of thing....


>         mode = os.stat(pathname)[ST_MODE]
>         if element.startswith('.'):
>             listDirectory.remove(element)
>             continue

Then instead of removing what you don't want, add 
the ones you do...

>         if S_ISDIR(mode):
>         elif S_ISREG(mode):
>     return listDirectory 

Just a thought.

Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld