newb question: file searching
jaysherby at gmail.com
jaysherby at gmail.com
Tue Aug 8 16:55:17 EDT 2006
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.
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
More information about the Python-list
mailing list