# The filecmp module is a portable way to check # whether two (or more) files are the same. # The module only has two methods: # cmp(file1, file2) # cmpfiles(directory1, directory2, common) import filecmp # find more verbose filecmp python docs here # check whether two files are the same # of course you need to have a /etc/hosts and .bak # for this example to work (feel free to change to filenames # that do exist on your setup) if filecmp.cmp('/etc/hosts', '/etc/hosts.bak'): print "the files are the same" else: print "the files are not the same" # filecmp also allows you to compare directories # you can use cmpfiles which returns 3 tuples: # matches, mismatch, error match, mismatch, error = filecmp.cmpfiles('folder1', 'folder2', ['LICENSE.TXT', 'README.TXT', 'VERSION']) print "Matching: ", match print "Mismatched: ", mismatch print "Errors: ", error # output (for me): # Matching: ['LICENSE.TXT', 'README.TXT'] # Mismatched: ['LICENSE'] # Errors: []
A python example based blog that shows how to accomplish python goals and how to correct python errors.
Showing posts with label cmp. Show all posts
Showing posts with label cmp. Show all posts
Thursday, October 1, 2009
Python - using filecmp to compare two or more files
Subscribe to:
Posts (Atom)