[Tutor] Lists of files

William O'Higgins william.ohiggins at utoronto.ca
Sat May 14 17:13:57 CEST 2005


Here's the problem - I want a list (array) of the files in a directory,
and then I want to iterate over the list testing for image-ness (with
imghdr.what()) and put all the image filenames in a global list.

What I've tried is this:

files = glob.glob('*.*')

for file in files:
    global pics 
    pics = []
    if imghdr.what(file):
        # so far so good - file is a list of files in the directory
        pics.append(file)
        # I this this is the problem - my list only has the last
        # alphabetical entry in it

So there's two questions - is there a better way to create a list of
files in a directory?  And, how do I populate my list to get all of the
filenames.  I've also tried "pics + [file]", but that gave me an empty
list.
-- 

yours,

William

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.python.org/pipermail/tutor/attachments/20050514/a4394620/attachment.pgp


More information about the Tutor mailing list