[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

Example 4-5. PP4E\System\Filetools\lister_recur.py


list files in dir tree by recursion


import sys, os


def mylister(currdir):
print('[' + currdir + ']')
for file in os.listdir(currdir): # list files here
path = os.path.join(currdir, file) # add dir path back
if not os.path.isdir(path):
print(path)
else:
mylister(path) # recur into subdirs


if name == 'main':
mylister(sys.argv[1]) # dir name in cmdline


As usual, this file can be both imported and called or run as a script, though the fact
that its result is printed text makes it less useful as an imported component unless its
output stream is captured by another program.


When run as a script, this file’s output is equivalent to that of Example 4-4, but not
identical—unlike the os.walk version, our recursive walker here doesn’t order the walk
to visit files before stepping into subdirectories. It could by looping through the file-
names list twice (selecting files first), but as coded, the order is dependent on os.list
dir results. For most use cases, the walk order would be irrelevant:


C:\...\PP4E\System\Filetools> python lister_recur.py C:\temp\test
[C:\temp\test]
[C:\temp\test\parts]
C:\temp\test\parts\part0001
C:\temp\test\parts\part0002
C:\temp\test\parts\part0003
C:\temp\test\parts\part0004
C:\temp\test\random.bin
C:\temp\test\spam.txt
C:\temp\test\temp.bin
C:\temp\test\temp.txt

We’ll make better use of most of this section’s techniques in later examples in Chap-
ter 6 and in this book at large. For example, scripts for copying and comparing directory
trees use the tree-walker techniques introduced here. Watch for these tools in action
along the way. We’ll also code a find utility in Chapter 6 that combines the tree traversal
of os.walk with the filename pattern expansion of glob.glob.


Handling Unicode Filenames in 3.X: listdir, walk, glob


Because all normal strings are Unicode in Python 3.X, the directory and file names
generated by os.listdir, os.walk, and glob.glob so far in this chapter are technically
Unicode strings. This can have some ramifications if your directories contain unusual
names that might not decode properly.


172 | Chapter 4: File and Directory Tools

Free download pdf