[Patches] find file permissions error
Guido van Rossum
guido@python.org
Tue, 29 Feb 2000 08:30:05 -0500
> > Find file has permissions error in Python 1.5.2. I add error correction
> > so it does not crash because of permission denied.
>
> First thing first: the function does not crash (else it would be a bug in
> the interpreter), but raises OSError. It could be troublesome or some
> users, but it could be necessary for others. If anything, the fix should
> be to receive an optional function object to call on errors, and if it is
> not given, continue to raise OSError.
In Python 1.6, find.py will be declared obsolete, and live in
Lib/lib-old (which means that it's not on the default module search
path).
Thus, changing the API seems bogus.
The reason for making it obsolete is that it really was a mistake to
add it in the first place; it looks like it was a utility used for the
Mac development at the time.
In 1.6, you can whip up the same thing using os.path.walk():
import os, fnmatch
def find(pat, dir=os.curdir):
list = []
def visit((list, pat), dir, names):
for name in names:
if fnmatch.fnmatch(name, pat):
fullname = os.path.join(dir, name)
list.append(fullname)
os.path.walk(dir, visit, (list, pat))
return list
By the way, has anybody else noticed some redundant logic in
posixpath.walk()? It contains code to skip "." and "..", but
os.listdir() stopped returning those many years ago...
--Guido van Rossum (home page: http://www.python.org/~guido/)