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

(yzsuai) #1

Splitting and joining listing results


In the last example, I pointed out that glob returns names with directory paths, whereas
listdir gives raw base filenames. For convenient processing, scripts often need to split
glob results into base files or expand listdir results into full paths. Such translations
are easy if we let the os.path module do all the work for us. For example, a script that
intends to copy all files elsewhere will typically need to first split off the base filenames
from glob results so that it can add different directory names on the front:


>>> dirname = r'C:\temp\parts'
>>>
>>> import glob
>>> for file in glob.glob(dirname + '/*'):
... head, tail = os.path.split(file)
... print(head, tail, '=>', ('C:\\Other\\' + tail))

C:\temp\parts part0001 => C:\Other\part0001
C:\temp\parts part0002 => C:\Other\part0002
C:\temp\parts part0003 => C:\Other\part0003
C:\temp\parts part0004 => C:\Other\part0004

Here, the names after the => represent names that files might be moved to. Conversely,
a script that means to process all files in a different directory than the one it runs in will
probably need to prepend listdir results with the target directory name before passing
filenames on to other tools:


>>> import os
>>> for file in os.listdir(dirname):
... print(dirname, file, '=>', os.path.join(dirname, file))

C:\temp\parts part0001 => C:\temp\parts\part0001
C:\temp\parts part0002 => C:\temp\parts\part0002
C:\temp\parts part0003 => C:\temp\parts\part0003
C:\temp\parts part0004 => C:\temp\parts\part0004

When you begin writing realistic directory processing tools of the sort we’ll develop in
Chapter 6, you’ll find these calls to be almost habit.


Walking Directory Trees


You may have noticed that almost all of the techniques in this section so far return the
names of files in only a single directory (globbing with more involved patterns is the
only exception). That’s fine for many tasks, but what if you want to apply an operation
to every file in every directory and subdirectory in an entire directory tree?


For instance, suppose again that we need to find every occurrence of a global name in
our Python scripts. This time, though, our scripts are arranged into a module pack-
age: a directory with nested subdirectories, which may have subdirectories of their own.
We could rerun our hypothetical single-directory searcher manually in every directory
in the tree, but that’s tedious, error prone, and just plain not fun.


168 | Chapter 4: File and Directory Tools

Free download pdf