newb question: file searching

Simon Forman rogue_pedro at yahoo.com
Tue Aug 8 17:27:29 EDT 2006


jaysherby at gmail.com wrote:
> hiaips wrote:
> > > jaysherby at gmail.com wrote:
> > > > I'm new at Python and I need a little advice.  Part of the script I'm
> > > > trying to write needs to be aware of all the files of a certain
> > > > extension in the script's path and all sub-directories.  Can someone
> > > > set me on the right path to what modules and calls to use to do that?
> > > > You'd think that it would be a fairly simple proposition, but I can't
> > > > find examples anywhere.  Thanks.
> >
> > dir_name = 'mydirectory'
> > extension = 'my extension'
> > import os
> > files = os.listdir(dir_name)
> > files_with_ext = [file for file in files if file.endswith(extension)]
> >
> > That will only do the top level (not subdirectories), but you can use
> > the os.walk procedure (or some of the other procedures in the os and
> > os.path modules) to do that.
> >
> > --Dave
>
> Thanks, Dave.  That's exactly what I was looking for, well, except for
> a few small alterations I'll make to achieve the desired effect.  I
> must ask, in the interest of learning, what is
>
> [file for file in files if file.endswith(extension)]
>
> actually doing?  I know that 'file' is a type, but what's with the set
> up and the brackets and all?  Can someone run down the syntax for me on
> that?  And also, I'm still not sure I know exactly how os.walk() works.
>  And, finally, the python docs all note that symbols like . and ..
> don't work with these commands.  How can I grab the directory that my
> script is residing in?
>
> Thanks.
>
>

'file' *is* the name of the file type, so one shouldn't reuse it as the
name of a variable as Dave did in his example.

the brackets are a "list comprehension" and they work like so:

files_with_ext = []
for filename in files:
    if filename.endswith(extension):
        files_with_ext.append(filename)

The above is exactly equivalent to the [filename for filename in
files... ] form.

AFAIK, os.listdir('.') works fine, but you can also use
os.listdir(os.getcwd()).  However, both of those return the current
directory, which can be different than the directory your script's
residing in if you've called os.chdir() or if you start your script
from a different directory.
HTH.

(Also, if you're not already, be aware of os.path.isfile() and
os.path.isdir().  They should probably be helpful to you.)

Peace,
~Simon




More information about the Python-list mailing list