Skip to main content
added 747 characters in body
Source Link
Jonathan H
  • 2.5k
  • 3
  • 24
  • 29
#!/usr/bin/env python import os import sys from termcolor import colored def compare_filestats(file1,file2): """ Compares modified time and size between two files. Return: -1 if file1 or file2 does not exist 0 if they exist and compare equal 1 if they have different modified time, but same size 2 if they have different size, but same modified time 3 if they have different size, and different modified time """ if not os.path.exists(file1) or not os.path.exists(file2): return -1 stat1 = os.stat(file1) stat2 = os.stat(file2) return (stat1.st_mtime != stat2.st_mtime) \  + 2*(stat1.st_size != stat2.st_size) def compare_folders(folder1,folder2): """ folder1: serves as reference and will be walked through folder2: serves as target and will be querried for each file in folder1 greenPrints meanscolored everystatus filefor foundeach file in folder1:  is  missing: file was not found in folderfolder2 2   with same filestats  yellow meansmtime that it: modified time is founddifferent  but with different filestats  redsize means that it: filesize is notdifferent ok  : found with same filestats """ for dirpath, dirnames, filenames in os.walk(folder1): for file1 in ( os.path.join(dirpath, x) for x in filenames ): relpath = file1[len(folder1):] file2 = os.path.join( folder2, relpath )   ifcomp not= os.path.existscompare_filestats(file1,file2) if comp < 0: colorstatus = colored('[missing]','red') elif compare_filestatscomp == 1: status = colored(file1'[mtime ]',file2'yellow') elif comp >= 2: colorstatus = colored('[size  ]','yellow') else: colorstatus = colored('[ok  ]','green') print colored(relpathstatus,color)  relpath if __name__ == '__main__': compare_folders(sys.argv[1],sys.argv[2]) 
#!/usr/bin/env python import os import sys from termcolor import colored def compare_filestats(file1,file2): stat1 = os.stat(file1) stat2 = os.stat(file2) return (stat1.st_mtime != stat2.st_mtime) + 2*(stat1.st_size != stat2.st_size) def compare_folders(folder1,folder2): """ folder1: serves as reference and will be walked through folder2: serves as target and will be querried for each file in folder1 green means every file found in folder1 is found in folder 2 with same filestats  yellow means that it is found but with different filestats  red means that it is not found """ for dirpath, dirnames, filenames in os.walk(folder1): for file1 in ( os.path.join(dirpath, x) for x in filenames ): relpath = file1[len(folder1):] file2 = os.path.join( folder2, relpath )   if not os.path.exists(file2): color = 'red' elif compare_filestats(file1,file2): color = 'yellow' else: color = 'green' print colored(relpath,color)  if __name__ == '__main__': compare_folders(sys.argv[1],sys.argv[2]) 
#!/usr/bin/env python import os import sys from termcolor import colored def compare_filestats(file1,file2): """ Compares modified time and size between two files. Return: -1 if file1 or file2 does not exist 0 if they exist and compare equal 1 if they have different modified time, but same size 2 if they have different size, but same modified time 3 if they have different size, and different modified time """ if not os.path.exists(file1) or not os.path.exists(file2): return -1 stat1 = os.stat(file1) stat2 = os.stat(file2) return (stat1.st_mtime != stat2.st_mtime) \  + 2*(stat1.st_size != stat2.st_size) def compare_folders(folder1,folder2): """ folder1: serves as reference and will be walked through folder2: serves as target and will be querried for each file in folder1 Prints colored status for each file in folder1:    missing: file was not found in folder2    mtime : modified time is different  size : filesize is different ok  : found with same filestats """ for dirpath, dirnames, filenames in os.walk(folder1): for file1 in ( os.path.join(dirpath, x) for x in filenames ): relpath = file1[len(folder1):] file2 = os.path.join( folder2, relpath ) comp = compare_filestats(file1,file2) if comp < 0: status = colored('[missing]','red') elif comp == 1: status = colored('[mtime ]','yellow') elif comp >= 2: status = colored('[size  ]','yellow') else: status = colored('[ok  ]','green') print status, relpath if __name__ == '__main__': compare_folders(sys.argv[1],sys.argv[2]) 
Source Link
Jonathan H
  • 2.5k
  • 3
  • 24
  • 29

Based on Chris Down's script, this script is a little more "visual". Calling it with two arguments folder1 and folder2, it walks the first folder and for each file searches a corresponding file in the second folder. If it is found, the relative path is printed in green, if they have different modified time or size, it is printed in yellow, and if it is not found then it is printed in red.

#!/usr/bin/env python import os import sys from termcolor import colored def compare_filestats(file1,file2): stat1 = os.stat(file1) stat2 = os.stat(file2) return (stat1.st_mtime != stat2.st_mtime) + 2*(stat1.st_size != stat2.st_size) def compare_folders(folder1,folder2): """ folder1: serves as reference and will be walked through folder2: serves as target and will be querried for each file in folder1 green means every file found in folder1 is found in folder 2 with same filestats yellow means that it is found but with different filestats red means that it is not found """ for dirpath, dirnames, filenames in os.walk(folder1): for file1 in ( os.path.join(dirpath, x) for x in filenames ): relpath = file1[len(folder1):] file2 = os.path.join( folder2, relpath ) if not os.path.exists(file2): color = 'red' elif compare_filestats(file1,file2): color = 'yellow' else: color = 'green' print colored(relpath,color) if __name__ == '__main__': compare_folders(sys.argv[1],sys.argv[2]) 

Note that this is not sufficient to decide whether the two folders are the same, you would need to run it both ways to make sure. In practice if you just want to know whether the folders are the same, then Chris' script is better. If you want to know what's missing or different from one folder to another, then my script will tell you.

NOTE: you will need termcolor installed, pip install termcolor.