break if bytes1 != bytes2: diffs.append('files differ at %s - %s' % (path1, path2)) print(name, 'DIFFERS') break # recur to compare directories in common for name in common: path1 = os.path.join(dir1, name) path2 = os.path.join(dir2, name) if os.path.isdir(path1) and os.path.isdir(path2): missed.remove(name) comparetrees(path1, path2, diffs, verbose) # same name but not both files or dirs? for name in missed: diffs.append('files missed at %s - %s: %s' % (dir1, dir2, name)) print(name, 'DIFFERS') if __name__ == '__main__': dir1, dir2 = dirdiff.getargs() diffs = [] comparetrees(dir1, dir2, diffs, True) # changes diffs in-place print('=' * 40) # walk, report diffs list if not diffs: print('No diffs found.') else: print('Diffs found:', len(diffs)) for diff in diffs: print('-', diff)
if bytes1 != bytes2: diffs.append('files differ at %s - %s' % (path1, path2)) print(name, 'DIFFERS') break # 递归比较共有目录 for name in common: path1 = os.path.join(dir1, name) path2 = os.path.join(dir2, name) if os.path.isdir(path1) and os.path.isdir(path2): missed.remove(name) comparetrees(path1, path2, diffs, verbose) # 同名但一个是文件,一个是目录 for name in missed: diffs.append('files missed at %s - %s: %s' % (dir1, dir2, name)) print(name, 'DIFFERS') if __name__ == '__main__': dir1, dir2 = dirdiff.getargs() diffs = [] comparetrees(dir1, dir2, diffs, True) print('=' * 40) if not diffs: print('No diffs found.') else: print('Diffs found:', len(diffs)) for diff in diffs: print('-', diff)