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

(yzsuai) #1
Comparing contents
bigext-tree.py matches
bigpy-dir.py matches
...more output omitted...
========================================
Diffs found: 7


  • files differ at F:\writing-backups\feb-26-10\dev\ch00.doc –
    C:\Users\mark\Stuff\Books\4E\PP4E\dev\ch00.doc

  • files differ at F:\writing-backups\feb-26-10\dev\ch02.doc –
    C:\Users\mark\Stuff\Books\4E\PP4E\dev\ch02.doc

  • files differ at F:\writing-backups\feb-26-10\dev\ch04.doc –
    C:\Users\mark\Stuff\Books\4E\PP4E\dev\ch04.doc

  • files differ at F:\writing-backups\feb-26-10\dev\ch06.doc –
    C:\Users\mark\Stuff\Books\4E\PP4E\dev\ch06.doc

  • files differ at F:\writing-backups\feb-26-10\dev\TOC.txt –
    C:\Users\mark\Stuff\Books\4E\PP4E\dev\TOC.txt

  • unique files at F:\writing-backups\feb-26-10\dev\Examples\PP4E\System\Filetools –
    C:\Users\mark\Stuff\Books\4E\PP4E\dev\Examples\PP4E\System\Filetools

  • files differ at F:\writing-backups\feb-26-10\dev\Examples\PP4E\Tools\visitor.py –
    C:\Users\mark\Stuff\Books\4E\PP4E\dev\Examples\PP4E\Tools\visitor.py


This particular run indicates that I’ve added a few examples and changed some chapter
files since the last backup; if run immediately after a backup, nothing should show up
on diffall radar except for any files that cannot be copied in general. This global
comparison can take a few minutes. It performs byte-for-byte comparisons of all chap-
ter files and screenshots, the examples tree, and more, but it’s an accurate and complete
verification. Given that this book development tree contained many files, a more man-
ual verification procedure without Python’s help would be utterly impossible.


After writing this script, I also started using it to verify full automated backups of my
laptops onto an external hard-drive device. To do so, I run the cpall copy script we
wrote earlier in the preceding section of this chapter, and then the comparison script
developed here to check results and get a list of files that didn’t copy correctly. The last
time I did this, this procedure copied and compared 225,000 files and 15,000 directories
in 20 GB of space—not the sort of task that lends itself to manual labor!


Here are the magic incantations on my Windows laptop. f:\ is a partition on my ex-
ternal hard drive, and you shouldn’t be surprised if each of these commands runs for
half an hour or more on currently common hardware. A drag-and-drop copy takes at
least as long (assuming it works at all!):


C:\...\System\Filetools> cpall.py c:\ f:\ > f:\copy-log.txt
C:\...\System\Filetools> diffall.py f:\ c:\ > f:\diff-log.txt

Reporting Differences and Other Ideas


Finally, it’s worth noting that this script still only detects differences in the tree but does
not give any further details about individual file differences. In fact, it simply loads and
compares the binary contents of corresponding files with string comparisons. It’s a
simple yes/no result.


Comparing Directory Trees | 317
Free download pdf