Re: [Tutor] Preffered way to search posix filesystem
Miles Stevenson wrote: 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. The docs are correct but maybe a little subtle. They say, walk( top[, topdown=True [, onerror=None]]) walk() generates the file names in a directory tree, by walking the tree either top down or bottom up. For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames). To someone familiar with Python generators, the words "generates" and "yields" are strong clues that walk is a generator and when you iterate over it you get tuples. If you are not familiar with this terminology I can see how it would be confusing. OTOH there are two examples in the same section that show correct usage... Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Preffered way to search posix filesystem
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): 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 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 [EMAIL PROTECTED] PGP FP: 035F 7D40 44A9 28FA 7453 BDF4 329F 889D 767D 2F63 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Preffered way to search posix filesystem
Try this def findfile(thefile, toplevel="C:\\windows",findcount=-1,subdirectories=True,returnlist=False): results = [] t = 0 if subdirectories: for root,dirs,files in os.walk(toplevel): for x in files: fullname = os.path.join(root,x) if x.lower() == thefile.lower(): print "Found %s" % fullname results.append(fullname+": 0:") t = t+1 else: print "Searching %s" % fullname if t == findcount: break print "\nFound %s matches:" % t pplist(results) else: n = 0 for x in os.listdir(toplevel): fullname = os.path.join(toplevel,x) if x.lower()==thefile.lower(): print "Found:\n%s: 0:" % fullname n = 1 results.append(fullname) break if not n: print "Found 0 matches." if returnlist: return results ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Preffered way to search posix filesystem
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 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Preffered way to search posix filesystem
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. -- Miles Stevenson [EMAIL PROTECTED] PGP FP: 035F 7D40 44A9 28FA 7453 BDF4 329F 889D 767D 2F63 pgpLQhrIGjLES.pgp Description: PGP signature ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor