[Tutor] Lists of files
Karl Pflästerer
sigurd at 12move.de
Sat May 14 20:17:03 CEST 2005
On 14 Mai 2005, william.ohiggins at utoronto.ca wrote:
> 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
The problem here is that in every iteration you set the list pics to
`[]'. If you wanted to solve the problem like above (not very nice; try
to avoid globals if possible) you had to define `pics' outside the loop.
Bernard gave you an answer how you could solve it with a simple list
comprehension (just filter the output from os.listdir); it can also be
written like that:
filter(imghdr.what, os.listdir('.'))
Just jump before to the directory you want to get searched with:
os.chdir(path)
If you want a more general solution which also allows to search
recursively through a directory tree you could use something like that:
def filter_files (root, filepred=lambda f: f, dirpred=lambda d: False):
filtered= []
jn = os.path.join
for path, dirs, files in os.walk(root, topdown=True):
for d in dirs:
if not dirpred(jn(path, d)): dirs.remove(d)
filtered.extend([jn(path,f) for f in files if filepred(jn(path, f))])
return filtered
The above takes two callback functions: filepred and dirpred. For every
file filepred returns a true value that file gets appended to the list
of returned files. `dirpred' allows you to recurse only in directories
where that function returns a true value.
So to have the same as above you write:
filter_files('.', filepred=imghdr.what)
and if you wanted to search a directory tree without e.g. the ./bin
directory you could write:
filter_files('.', filepred=imghdr.what, dirpred=lambda d: not d.endswith('bin'))
HTH
Karl
--
Please do *not* send copies of replies to me.
I read the list
More information about the Tutor
mailing list