Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
068bdb18
Commit
068bdb18
authored
Aug 03, 1999
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Change the directory tree walking example to use clearer variable
names, some suggested by Joe Ellsworth.
parent
6a619f44
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
14 additions
and
11 deletions
+14
-11
Doc/lib/libstat.tex
Doc/lib/libstat.tex
+14
-11
No files found.
Doc/lib/libstat.tex
View file @
068bdb18
...
...
@@ -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}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment