mirror of
https://github.com/python/cpython.git
synced 2025-08-02 16:13:13 +00:00
Change the directory tree walking example to use clearer variable
names, some suggested by Joe Ellsworth.
This commit is contained in:
parent
6a619f44c5
commit
068bdb181d
1 changed files with 14 additions and 11 deletions
|
@ -118,23 +118,26 @@ Example:
|
||||||
import os, sys
|
import os, sys
|
||||||
from stat import *
|
from stat import *
|
||||||
|
|
||||||
def process(dir, func):
|
def walktree(dir, callback):
|
||||||
'''recursively descend the directory rooted at dir, calling func for
|
'''recursively descend the directory rooted at dir,
|
||||||
each regular file'''
|
calling the callback function for each regular file'''
|
||||||
|
|
||||||
for f in os.listdir(dir):
|
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):
|
if S_ISDIR(mode):
|
||||||
# recurse into directory
|
# It's a directory, recurse into it
|
||||||
process('%s/%s' % (dir, f), func)
|
walktree(pathname, callback)
|
||||||
elif S_ISREG(mode):
|
elif S_ISREG(mode):
|
||||||
func('%s/%s' % (dir, f))
|
# It's a file, call the callback function
|
||||||
|
callback(pathname)
|
||||||
else:
|
else:
|
||||||
print 'Skipping %s/%s' % (dir, f)
|
# Unknown file type, print a message
|
||||||
|
print 'Skipping %s' % pathname
|
||||||
|
|
||||||
def f(file):
|
def visitfile(file):
|
||||||
print 'frobbed', file
|
print 'visiting', file
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
process(sys.argv[1], f)
|
walktree(sys.argv[1], visitfile)
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue