diff options
author | Guido van Rossum <guido@python.org> | 1999-08-03 21:52:29 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1999-08-03 21:52:29 (GMT) |
commit | 068bdb181defc31b3b4c57966f48c4c11330b681 (patch) | |
tree | 5ed81c0228e6acde987921e0fa49365a5f60aa77 /Doc/lib | |
parent | 6a619f44c567efe859b4b94a710b718d475a57fe (diff) | |
download | cpython-068bdb181defc31b3b4c57966f48c4c11330b681.zip cpython-068bdb181defc31b3b4c57966f48c4c11330b681.tar.gz cpython-068bdb181defc31b3b4c57966f48c4c11330b681.tar.bz2 |
Change the directory tree walking example to use clearer variable
names, some suggested by Joe Ellsworth.
Diffstat (limited to 'Doc/lib')
-rw-r--r-- | Doc/lib/libstat.tex | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/Doc/lib/libstat.tex b/Doc/lib/libstat.tex index 596e06c..7d19a68 100644 --- a/Doc/lib/libstat.tex +++ b/Doc/lib/libstat.tex @@ -118,23 +118,26 @@ Example: import os, sys from stat import * -def process(dir, func): - '''recursively descend the directory rooted at dir, calling func for - each regular file''' +def walktree(dir, callback): + '''recursively descend the directory rooted at dir, + calling the callback function for each regular file''' for f in os.listdir(dir): - mode = os.stat('%s/%s' % (dir, f))[ST_MODE] + pathname = '%s/%s' % (dir, f) + mode = os.stat(pathname)[ST_MODE] if S_ISDIR(mode): - # recurse into directory - process('%s/%s' % (dir, f), func) + # It's a directory, recurse into it + walktree(pathname, callback) elif S_ISREG(mode): - func('%s/%s' % (dir, f)) + # It's a file, call the callback function + callback(pathname) else: - print 'Skipping %s/%s' % (dir, f) + # Unknown file type, print a message + print 'Skipping %s' % pathname -def f(file): - print 'frobbed', file +def visitfile(file): + print 'visiting', file if __name__ == '__main__': - process(sys.argv[1], f) + walktree(sys.argv[1], visitfile) \end{verbatim} |