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

(yzsuai) #1

Running the Script


Since we’ve already studied the tree-walking tools this script employs, let’s jump right
into a few example runs. When run on identical trees, status messages scroll during the
traversal, and a No diffs found. message appears at the end:


C:\...\PP4E\System\Filetools> diffall.py C:\temp\PP3E\Examples copytemp > diffs.txt
C:\...\PP4E\System\Filetools> type diffs.txt | more
--------------------
Comparing C:\temp\PP3E\Examples to copytemp
Directory lists are identical
Comparing contents
README-root.txt matches
--------------------
Comparing C:\temp\PP3E\Examples\PP3E to copytemp\PP3E
Directory lists are identical
Comparing contents
echoEnvironment.pyw matches
LaunchBrowser.pyw matches
Launcher.py matches
Launcher.pyc matches
...over 2,000 more lines omitted...
--------------------
Comparing C:\temp\PP3E\Examples\PP3E\TempParts to copytemp\PP3E\TempParts
Directory lists are identical
Comparing contents
109_0237.JPG matches
lawnlake1-jan-03.jpg matches
part-001.txt matches
part-002.html matches
========================================
No diffs found.

I usually run this with the verbose flag passed in as True, and redirect output to a file
(for big trees, it produces too much output to scroll through comfortably); use False
to watch fewer status messages fly by. To show how differences are reported, we need
to generate a few; for simplicity, I’ll manually change a few files scattered about one of
the trees, but you could also run a global search-and-replace script like the one we’ll
write later in this chapter. While we’re at it, let’s remove a few common files so that
directory uniqueness differences show up on the scope, too; the last two removal com-
mands in the following will generate one difference in the same directory in different
trees:


C:\...\PP4E\System\Filetools> notepad copytemp\PP3E\README-PP3E.txt
C:\...\PP4E\System\Filetools> notepad copytemp\PP3E\System\Filetools\commands.py
C:\...\PP4E\System\Filetools> notepad C:\temp\PP3E\Examples\PP3E\__init__.py

C:\...\PP4E\System\Filetools> del copytemp\PP3E\System\Filetools\cpall_visitor.py
C:\...\PP4E\System\Filetools> del copytemp\PP3E\Launcher.py
C:\...\PP4E\System\Filetools> del C:\temp\PP3E\Examples\PP3E\PyGadgets.py

Now, rerun the comparison walker to pick out differences and redirect its output report
to a file for easy inspection. The following lists just the parts of the output report that


314 | Chapter 6: Complete System Programs

Free download pdf