filecmp
--- 檔案與目錄比較¶
原始碼:Lib/filecmp.py
Thefilecmp
module defines functions to compare files and directories,with various optional time/correctness trade-offs. For comparing files,see also thedifflib
module.
Thefilecmp
module defines the following functions:
- filecmp.cmp(f1,f2,shallow=True)¶
Compare the files namedf1 andf2, returning
True
if they seem equal,False
otherwise.Ifshallow is true and the
os.stat()
signatures (file type, size, andmodification time) of both files are identical, the files are taken to beequal.Otherwise, the files are treated as different if their sizes or contents differ.
Note that no external programs are called from this function, giving itportability and efficiency.
This function uses a cache for past comparisons and the results,with cache entries invalidated if the
os.stat()
information for thefile changes. The entire cache may be cleared usingclear_cache()
.
- filecmp.cmpfiles(dir1,dir2,common,shallow=True)¶
Compare the files in the two directoriesdir1 anddir2 whose names aregiven bycommon.
Returns three lists of file names:match,mismatch,errors.match contains the list of files that match,mismatch containsthe names of those that don't, anderrors lists the names of files whichcould not be compared. Files are listed inerrors if they don't exist inone of the directories, the user lacks permission to read them or if thecomparison could not be done for some other reason.
Theshallow parameter has the same meaning and default value as for
filecmp.cmp()
.For example,
cmpfiles('a','b',['c','d/e'])
will comparea/c
withb/c
anda/d/e
withb/d/e
.'c'
and'd/e'
will each be inone of the three returned lists.
- filecmp.clear_cache()¶
Clear the filecmp cache. This may be useful if a file is compared so quicklyafter it is modified that it is within the mtime resolution ofthe underlying filesystem.
在 3.4 版被加入.
Thedircmp
class¶
- classfilecmp.dircmp(a,b,ignore=None,hide=None,*,shallow=True)¶
Construct a new directory comparison object, to compare the directoriesaandb.ignore is a list of names to ignore, and defaults to
filecmp.DEFAULT_IGNORES
.hide is a list of names to hide, anddefaults to[os.curdir,os.pardir]
.The
dircmp
class compares files by doingshallow comparisonsas described forfilecmp.cmp()
by default using theshallowparameter.在 3.13 版的變更:Added theshallow parameter.
The
dircmp
class provides the following methods:- report()¶
Print (to
sys.stdout
) a comparison betweena andb.
- report_partial_closure()¶
Print a comparison betweena andb and common immediatesubdirectories.
- report_full_closure()¶
Print a comparison betweena andb and common subdirectories(recursively).
The
dircmp
class offers a number of interesting attributes that may beused to get various bits of information about the directory trees beingcompared.Note that via
__getattr__()
hooks, all attributes are computed lazily,so there is no speed penalty if only those attributes which are lightweightto compute are used.- left¶
The directorya.
- right¶
The directoryb.
- left_list¶
Files and subdirectories ina, filtered byhide andignore.
- right_list¶
Files and subdirectories inb, filtered byhide andignore.
- common¶
Files and subdirectories in botha andb.
- left_only¶
Files and subdirectories only ina.
- right_only¶
Files and subdirectories only inb.
- common_dirs¶
Subdirectories in botha andb.
- common_files¶
Files in botha andb.
- common_funny¶
Names in botha andb, such that the type differs between thedirectories, or names for which
os.stat()
reports an error.
- same_files¶
Files which are identical in botha andb, using the class'sfile comparison operator.
- diff_files¶
Files which are in botha andb, whose contents differ accordingto the class's file comparison operator.
- funny_files¶
Files which are in botha andb, but could not be compared.
- subdirs¶
A dictionary mapping names in
common_dirs
todircmp
instances (or MyDirCmp instances if this instance is of type MyDirCmp, asubclass ofdircmp
).
Here is a simplified example of using thesubdirs
attribute to searchrecursively through two directories to show common different files:
>>>fromfilecmpimportdircmp>>>defprint_diff_files(dcmp):...fornameindcmp.diff_files:...print("diff_file%s found in%s and%s"%(name,dcmp.left,...dcmp.right))...forsub_dcmpindcmp.subdirs.values():...print_diff_files(sub_dcmp)...>>>dcmp=dircmp('dir1','dir2')>>>print_diff_files(dcmp)