[Tutor] Preffered way to search posix filesystem
Miles Stevenson
miles at mstevenson.org
Fri Jan 28 17:00:30 CET 2005
Thanks for the advice! My problem was that the built-in Python docs in Kdevelop weren't up-to-date, and
I had trouble finding walk() in the docs. Here is the approach that I used being a python newbie
(improvements are welcome):
<wordwrap=OFF>
def getfiles(path):
"""Recursively search a path and generate a lit of OGG files found
Takes a filesystem path as an argument. The path is recursively searched for
OGG files. Returns a list of each file found (in absolute path format) with
the first element of the list set to 'start'"""
filelist = ['start']
for root, dirs, files in os.walk(path):
for name in files:
a = os.path.join(root, name)
if os.path.isfile(a) and fnmatch.fnmatch(a, '*.ogg'):
filelist.append(a)
return filelist
</wordwrap>
What is interesting is that the latest 2.4 Python docs say that walk() returns a Tuple, which is untrue.
It returns a generator object according to type(). This had me heavily confused as to how to use
what was returned from walk() and it took a good hour of troubleshooting to figure it out.
-Miles
On Wednesday 26 January 2005 11:27 pm, you wrote:
> Try the os module. I think this should probably get you there.
> http://docs.python.org/lib/module-os.html
>
> Miles Stevenson wrote:
> >I would like to search filesystem structures using globs on Posix
>
> systems from
>
> >within Python. I don't see an obvious method to do this with in the
>
> standard
>
> >modules. What is the preferred way of doing this? Should I just use
>
> the find
>
> >command or is there a good module out there for searching?
> >
> >Thanks.
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
--
Miles Stevenson
miles at mstevenson.org
PGP FP: 035F 7D40 44A9 28FA 7453 BDF4 329F 889D 767D 2F63
More information about the Tutor
mailing list