Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 068bdb1

Browse files
committed
Change the directory tree walking example to use clearer variable
names, some suggested by Joe Ellsworth.
1 parent 6a619f4 commit 068bdb1

1 file changed

Lines changed: 14 additions & 11 deletions

File tree

Doc/lib/libstat.tex

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,23 +118,26 @@ \section{\module{stat} ---
118118
import os, sys
119119
from stat import *
120120
121-
def process(dir, func):
122-
'''recursively descend the directory rooted at dir, calling func for
123-
each regular file'''
121+
def walktree(dir, callback):
122+
'''recursively descend the directory rooted at dir,
123+
calling the callback function for each regular file'''
124124
125125
for f in os.listdir(dir):
126-
mode = os.stat('%s/%s' % (dir, f))[ST_MODE]
126+
pathname = '%s/%s' % (dir, f)
127+
mode = os.stat(pathname)[ST_MODE]
127128
if S_ISDIR(mode):
128-
# recurse into directory
129-
process('%s/%s' % (dir, f), func)
129+
# It's a directory, recurse into it
130+
walktree(pathname, callback)
130131
elif S_ISREG(mode):
131-
func('%s/%s' % (dir, f))
132+
# It's a file, call the callback function
133+
callback(pathname)
132134
else:
133-
print 'Skipping %s/%s' % (dir, f)
135+
# Unknown file type, print a message
136+
print 'Skipping %s' % pathname
134137
135-
def f(file):
136-
print 'frobbed', file
138+
def visitfile(file):
139+
print 'visiting', file
137140
138141
if __name__ == '__main__':
139-
process(sys.argv[1], f)
142+
walktree(sys.argv[1], visitfile)
140143
\end{verbatim}

0 commit comments

Comments
 (0)