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

(yzsuai) #1

commonly used today. If you simply copy your script to any machine you wish to use
it on, it will work regardless of which other tools are available there; all you need is
Python. Moreover, coding such tasks in Python also allows you to perform arbitrary
actions along the way—replacements, deletions, and whatever else you can code in the
Python language.


Walking One Directory


The most common way to go about writing such tools is to first grab a list of the names
of the files you wish to process, and then step through that list with a Python for loop
or other iteration tool, processing each file in turn. The trick we need to learn here,
then, is how to get such a directory list within our scripts. For scanning directories there
are at least three options: running shell listing commands with os.popen, matching
filename patterns with glob.glob, and getting directory listings with os.listdir. They
vary in interface, result format, and portability.


Running shell listing commands with os.popen


How did you go about getting directory file listings before you heard of Python? If you’re
new to shell tools programming, the answer may be “Well, I started a Windows file
explorer and clicked on things,” but I’m thinking here in terms of less GUI-oriented
command-line mechanisms.


On Unix, directory listings are usually obtained by typing ls in a shell; on Windows,
they can be generated with a dir command typed in an MS-DOS console box. Because
Python scripts may use os.popen to run any command line that we can type in a shell,
they are the most general way to grab a directory listing inside a Python program. We
met os.popen in the prior chapters; it runs a shell command string and gives us a file
object from which we can read the command’s output. To illustrate, let’s first assume
the following directory structures—I have both the usual dir and a Unix-like ls com-
mand from Cygwin on my Windows laptop:


c:\temp> dir /B
parts
PP3E
random.bin
spam.txt
temp.bin
temp.txt

c:\temp> c:\cygwin\bin\ls
PP3E parts random.bin spam.txt temp.bin temp.txt

c:\temp> c:\cygwin\bin\ls parts
part0001 part0002 part0003 part0004

The parts and PP3E names are a nested subdirectory in C:\temp here (the latter is a copy
of the prior edition’s examples tree, which I used occasionally in this text). Now, as


164 | Chapter 4: File and Directory Tools

Free download pdf