def comparedirs(dir1, dir2, diffs, verbose=0): # compare filename lists print '-' * 20 if not dirdiff.comparedirs(dir1, dir2): diffs.append('unique files at %s - %s' % (dir1, dir2)) print 'Comparing contents' files1 = os.listdir(dir1) files2 = os.listdir(dir2) common = intersect(files1, files2) # compare contents of files in common for file in common: path1 = os.path.join(dir1, file) path2 = os.path.join(dir2, file) if os.path.isfile(path1) and os.path.isfile(path2): bytes1 = open(path1, 'rb').read() bytes2 = open(path2, 'rb').read() if bytes1 == bytes2: if verbose: print file, 'matches' else: diffs.append('files differ at %s - %s' % (path1, path2)) print file, 'DIFFERS' # recur to compare directories in common for file in common: path1 = os.path.join(dir1, file) path2 = os.path.join(dir2, file) if os.path.isdir(path1) and os.path.isdir(path2): comparedirs(path1, path2, diffs, verbose)
def comparedirs(dir1, dir2, diffs, verbose=0): # compare filename lists print '-'*20 if not dirdiff.comparedirs(dir1, dir2): diffs.append('unique files at %s - %s' % (dir1, dir2)) print 'Comparing contents' files1 = os.listdir(dir1) files2 = os.listdir(dir2) common = intersect(files1, files2) # compare contents of files in common for file in common: path1 = os.path.join(dir1, file) path2 = os.path.join(dir2, file) if os.path.isfile(path1) and os.path.isfile(path2): bytes1 = open(path1, 'rb').read() bytes2 = open(path2, 'rb').read() if bytes1 == bytes2: if verbose: print file, 'matches' else: diffs.append('files differ at %s - %s' % (path1, path2)) print file, 'DIFFERS' # recur to compare directories in common for file in common: path1 = os.path.join(dir1, file) path2 = os.path.join(dir2, file) if os.path.isdir(path1) and os.path.isdir(path2): comparedirs(path1, path2, diffs, verbose)
def comparetrees(dir1, dir2, diffs, verbose=False): """ Сравнивает все подкаталоги и файлы в двух деревьях каталогов; для предотвращения кодирования/декодирования содержимого и преобразования символов конца строки использует двоичный режим доступа к файлам, так как деревья могут содержать произвольные двоичные и текстовые файлы; функции listdir может потребоваться передавать аргумент типа bytes, если могут встречаться имена файлов, недекодируемые на других платформах """ # сравнить списки с именами файлов print('-' * 20) names1 = os.listdir(dir1) names2 = os.listdir(dir2) if not dirdiff.comparedirs(dir1, dir2, names1, names2): diffs.append('unique files at %s - %s' % (dir1, dir2)) print('Comparing contents') common = intersect(names1, names2) missed = common[:] # сравнить содержимое файлов с одинаковыми именами for name in common: path1 = os.path.join(dir1, name) path2 = os.path.join(dir2, name) if os.path.isfile(path1) and os.path.isfile(path2): missed.remove(name) file1 = open(path1, 'rb') file2 = open(path2, 'rb') while True: bytes1 = file1.read(blocksize) bytes2 = file2.read(blocksize) if (not bytes1) and (not bytes2): if verbose: print(name, 'matches') break 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')
def comparetrees(dir1, dir2, diffs, verbose=False): """ Compare all subdirectories and files in two directory trees; uses binary files to prevent Unicode decoding and endline transforms, as trees might contain arbitrary binary files as well as arbitrary text; may need bytes listdir arg for undecodable filenames on some platforms """ # compare file name lists print('-' * 20) names1 = os.listdir(dir1) names2 = os.listdir(dir2) if not dirdiff.comparedirs(dir1, dir2, names1, names2): diffs.append('unique files at %s - %s' % (dir1, dir2)) print('Comparing contents') common = intersect(names1, names2) missed = common[:] # compare contents of files in common for name in common: path1 = os.path.join(dir1, name) path2 = os.path.join(dir2, name) if os.path.isfile(path1) and os.path.isfile(path2): missed.remove(name) file1 = open(path1, 'rb') file2 = open(path2, 'rb') while True: bytes1 = file1.read(blocksize) bytes2 = file2.read(blocksize) if (not bytes1) and (not bytes2): if verbose: print(name, 'matches') 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')
def comparetrees(dir1, dir2, diffs, verbose=False): """ 比较两个目录树的所有子目录和文件; 使用二进制文件来阻止Unicode解码 和换行符转换 """ # 比较文件名列表 print('-' * 80) names1 = os.listdir(dir1) names2 = os.listdir(dir2) if not dirdiff.comparedirs(dir1, dir2, names1, names2): diffs.append('unique files at %s - %s' % (dir1, dir2)) print('Comparing contents') common = intersect(names1, names2) missed = common[:] # 比较共有文件内容 for name in common: path1 = os.path.join(dir1, name) path2 = os.path.join(dir2, name) if os.path.isfile(path1) and os.path.isfile(path2): missed.remove(name) file1 = open(path1, 'rb') file2 = open(path2, 'rb') while True: bytes1 = file1.read(blocksize) bytes2 = file2.read(blocksize) if (not bytes1) and (not bytes2): if verbose: print(name, 'matches') break 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')
def comparedirs(dir1, dir2, diffs, verbose=False): # compare filename lists print '-'*20 if not dirdiff.comparedirs(dir1, dir2): diffs.append('unique files at %s - %s' % (dir1, dir2)) print 'Comparing contents' names1 = os.listdir(dir1) names2 = os.listdir(dir2) common = intersect(names1, names2) missed = common[:] # compare contents of files in common for name in common: path1 = os.path.join(dir1, name) path2 = os.path.join(dir2, name) if os.path.isfile(path1) and os.path.isfile(path2): missed.remove(name) file1 = open(path1, 'rb') file2 = open(path2, 'rb') while True: bytes1 = file1.read(blocksize) bytes2 = file2.read(blocksize) if (not bytes1) and (not bytes2): if verbose: print name, 'matches' 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) comparedirs(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'
def comparedirs(dir1, dir2, diffs, verbose=False): # compare filename lists print '-' * 20 if not dirdiff.comparedirs(dir1, dir2): diffs.append('unique files at %s - %s' % (dir1, dir2)) print 'Comparing contents' names1 = os.listdir(dir1) names2 = os.listdir(dir2) common = intersect(names1, names2) missed = common[:] # compare contents of files in common for name in common: path1 = os.path.join(dir1, name) path2 = os.path.join(dir2, name) if os.path.isfile(path1) and os.path.isfile(path2): missed.remove(name) file1 = open(path1, 'rb') file2 = open(path2, 'rb') while True: bytes1 = file1.read(blocksize) bytes2 = file2.read(blocksize) if (not bytes1) and (not bytes2): if verbose: print name, 'matches' 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) comparedirs(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'
def comparedirs(dir1, dir2, diffs, verbose=False): print('-' * 20) names1 = os.listdir(dir1) names2 = os.listdir(dir2) if not dirdiff.comparedirs(dir1, dir2, names1, names2): diffs.append('unique files at %s - %s' % (dir1, dir2)) print('Comparing contents') common = intersect(names1, names2) missed = common[:] # file & file for name in common: path1 = os.path.join(dir1, name) path2 = os.path.join(dir2, name) if os.path.isfile(path1) and os.path.isfile(path2): missed.remove(name) file1 = open(path1, 'rb') file2 = open(path2, 'rb') while True: bytes1 = file1.read(blocksize) bytes2 = file2.read(blocksize) if (not bytes1) and (not bytes2): if verbose: print(name, 'matches') break if bytes1 != bytes2: diffs.append('files differ at %s - %s' % (path1, path2)) print(name, 'DIFFERS') break # dir & dir 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) comparedirs(path1, path2, diffs, verbose) # file & dir for name in missed: diffs.append('files missed at %s - %s: %s' % (dir1, dir2, name)) print(name, 'DIFFERS')
def comparetrees(dir1, dir2, diffs, verbose=False): print('-' * 20) names1 = os.listdir(dir1) names2 = os.listdir(dir2) if not dirdiff.comparedirs(dir1, dir2, names1, names2): diffs.append('Unique files at %s - %s' % (dir1, dir2)) print('<Comparing contents>') common = intersect(names1, names2) missed = common[:] for name in common: path1 = os.path.join(dir1, name) path2 = os.path.join(dir2, name) if os.path.isfile(path1) and os.path.isfile(path2): missed.remove(name) file1 = open(path1, 'rb') file2 = open(path2, 'rb') while True: bytes1 = file1.read(blocksize) bytes2 = file2.read(blocksize) if (not bytes1) and (not bytes2): if verbose: print(name, '- matches') break 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')
def comparetrees(dir1, dir2, diffs, verbose=False): print "-" * 20 names1 = os.listdir(dir1) names2 = os.listdir(dir2) if not dirdiff.comparedirs(dir1, dir2, names1, names2): diffs.append("unique file at %s - %s" % (dir1, dir2)) print "comparing contents..." common = intersect(names1, names2) missed = common[:] for name in common: path1 = os.path.join(dir1, name) path2 = os.path.join(dir2, name) if os.path.isfile(path1) and os.path.isfile(path2): missed.remove(name) file1 = open(path1, "rb") file2 = open(path2, "rb") while True: bytes1 = file1.read(blockSize) bytes2 = file2.read(blockSize) if (not bytes1) and (not bytes2): if verbose: print name, "mathes" break if bytes1 != bytes2: diffs.append("file 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("file missed at %s-%s: %s" % (dir1, dir2, name)) print name, "differ"