我们从Python开源项目中,提取了以下13个代码示例,用于说明如何使用filecmp.cmpfiles()。
def test_cmpfiles(self): self.assertTrue(filecmp.cmpfiles(self.dir, self.dir, ['file']) == (['file'], [], []), "Comparing directory to itself fails") self.assertTrue(filecmp.cmpfiles(self.dir, self.dir_same, ['file']) == (['file'], [], []), "Comparing directory to same fails") # Try it with shallow=False self.assertTrue(filecmp.cmpfiles(self.dir, self.dir, ['file'], shallow=False) == (['file'], [], []), "Comparing directory to itself fails") self.assertTrue(filecmp.cmpfiles(self.dir, self.dir_same, ['file'], shallow=False), "Comparing directory to same fails") # Add different file2 with open(os.path.join(self.dir, 'file2'), 'w') as output: output.write('Different contents.\n') self.assertFalse(filecmp.cmpfiles(self.dir, self.dir_same, ['file', 'file2']) == (['file'], ['file2'], []), "Comparing mismatched directories fails")
def phase3(self): """ Find out differences between common files. Ensure we are using content comparison with shallow=False. """ fcomp = filecmp.cmpfiles(self.left, self.right, self.common_files, shallow=False) self.same_files, self.diff_files, self.funny_files = fcomp
def test_cmpfiles(self): self.assertTrue(filecmp.cmpfiles(self.dir, self.dir, ['file']) == (['file'], [], []), "Comparing directory to itself fails") self.assertTrue(filecmp.cmpfiles(self.dir, self.dir_same, ['file']) == (['file'], [], []), "Comparing directory to same fails") # Try it with shallow=False self.assertTrue(filecmp.cmpfiles(self.dir, self.dir, ['file'], shallow=False) == (['file'], [], []), "Comparing directory to itself fails") self.assertTrue(filecmp.cmpfiles(self.dir, self.dir_same, ['file'], shallow=False), "Comparing directory to same fails") # Add different file2 output = open(os.path.join(self.dir, 'file2'), 'w') output.write('Different contents.\n') output.close() self.assertFalse(filecmp.cmpfiles(self.dir, self.dir_same, ['file', 'file2']) == (['file'], ['file2'], []), "Comparing mismatched directories fails")
def report_difference(casedirpath): # get the directories to be compared refpath = os.path.join(casedirpath, "ref") outpath = os.path.join(casedirpath, "out") if not os.path.isdir(refpath): print "Test case has no reference directory" return if not os.path.isdir(refpath): print "Test case has no output directory" return # check for recursive subdirectories if len(filter(lambda fn: os.path.isdir(fn), os.listdir(refpath))) > 0: print "Reference directory contains a subdirectory" return if len(filter(lambda fn: os.path.isdir(fn), os.listdir(outpath))) > 0: print "Output directory contains a sub directory" return # verify list of filenames dircomp = filecmp.dircmp(outpath, refpath, ignore=['.DS_Store']) if (len(dircomp.left_only) > 0): print "Output contains " + str(len(dircomp.left_only)) + " extra file(s)" if (len(dircomp.right_only) > 0): print "Output misses " + str(len(dircomp.right_only)) + " file(s)" # compare common files matches, mismatches, errors = filecmp.cmpfiles(outpath, refpath, dircomp.common, shallow=False) for filename in matches: print "Output file matches: " + filename for filename in mismatches + errors: if equalfiles(os.path.join(outpath, filename), os.path.join(refpath, filename)): print "Output file matches: " + filename else: print "Output file differs: " + filename + " <-------" # ----------------------------------------------------------------- # get the command-line argument specifying the target directory, if any
def _finddifference(self, casedirpath): # Get the full output and reference paths outpath = os.path.join(casedirpath, "out") refpath = os.path.join(casedirpath, "ref") # Check for the presence of the reference directory if not os.path.isdir(refpath): return "Test case has no reference directory" # Initialize lists to contain the extra files, missing files and differing files extra = [] missing = [] differ = [] # Verify list of filenames if len(filter(lambda fn: os.path.isdir(fn), os.listdir(outpath))) > 0: return "Output contains a directory" dircomp = filecmp.dircmp(outpath, refpath, ignore=['.DS_Store']) # Create a list of files that were not present in the reference directory for file in dircomp.left_only: extra.append(file) # Create a list of files that are missing from the output directory for file in dircomp.right_only: missing.append(file) # Compare files, focusing on those that aren't trivially equal. matches, mismatches, errors = filecmp.cmpfiles(outpath, refpath, dircomp.common, shallow=False) for filename in mismatches + errors: reffile = os.path.join(refpath, filename) outfile = os.path.join(outpath, filename) # For a soft comparison between files, check if both files are similar enough if self._parallel: if not similarfiles(reffile, outfile): differ.append(filename) # In the other case, check if both files are equal (except for potential timestamps) else: if not equalfiles(reffile, outfile): differ.append(filename) # Return the list of extra files, the list of missing files and the list of differing files return extra, missing, differ # ----------------------------------------------------------------- # This functions searches for directories within a certain parent directories that have a specific name