[EMAIL PROTECTED] wrote:
> I do appreciate the advice, but I've got a 12 line function that does
> all of that.  And it works!  I just wish I understood a particular line
> of it.
>
> def getFileList(*extensions):
>     import os
>     imageList = []
>     for dirpath, dirnames, files in os.walk('.'):
>           for filename in files:
>                   name, ext = os.path.splitext(filename)
>                   if ext.lower() in extensions and not 
> filename.startswith('.'):
>                           imageList.append(os.path.join(dirpath, filename))
>           for dirname in reversed(range(len(dirnames))):
>                   if dirnames[dirname].startswith('.'):
>                           del dirnames[dirname]
>
>     return imageList
>
> print getFileList('.jpg', '.gif', '.png')
>
> The line I don't understand is:
> reversed(range(len(dirnames)))
>

For a start, change "dirname" to "dirindex" (without changing
"dirnames"!) in that line and the next two lines -- this may help your
understanding.

The purpose of that loop is to eliminate from dirnames any entries
which start with ".". This needs to be done in-situ -- concocting a new
list and binding the name "dirnames" to it won't work.
The safest understandable way to delete entries from a list while
iterating over it is to do it backwards.

Doing it forwards doesn't always work; example:

#>>> dirnames = ['foo', 'bar', 'zot']
#>>> for x in range(len(dirnames)):
...     if dirnames[x] == 'bar':
...         del dirnames[x]
...
Traceback (most recent call last):
  File "<stdin>", line 2, in ?
IndexError: list index out of range

HTH,
John

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to