Compare Files

  • Set a directory that contains the "master" files to compare to
  • Add directories to compare to the list (see code for examples)
  • Generates a ".txt" file for directory containing missing files
  • Optionally, set "delete_missing" to 'yes' to delete files that do not match the "master" files

import os
import glob

#Directory to compare all others against (with no leading '/')
master_dir='/home/user/NAS/RomProject/temp/videos'

#List of directories to compare against the 'master_dir' (with no leading '/')
check_dirs=['/user/crow/NAS/RomProject/temp/roms','/home/user/NAS/RomProject/temp/art']

#Where to save the result .txt files
results_path='/home/user/NAS/RomProject/temp'

#If set to 'yes', deletes files found in 'check_dirs' that are not in the 'master_dir'
delete_missing='no'

#Create blank master list
master_list=[]

#Does the magic...
for each_master_file in os.listdir(master_dir):
	master_list.append(os.path.splitext(each_master_file)[0])

master_list.sort()

for each_check_dir in check_dirs:

	#Create blank directory list to compare to
	check_list=[]

	for each_check_file in os.listdir(each_check_dir):
		check_list.append(os.path.splitext(each_check_file)[0])

	check_list.sort()

	for each_file in master_list:
		if each_file not in check_list:

			#Saves a .txt file with each missing file labeled as the current 'check_dir' name
			with open('%s/%s.txt'%(results_path,os.path.basename(each_check_dir)),'a') as outfile:
				outfile.write('%s\n'%each_file)

	if delete_missing=='yes':
		for each_check_file in check_list:
			if each_check_file not in master_list:
				for current_file in glob.glob('%s/%s.*'%(each_check_dir,each_check_file)):
					os.remove(current_file)
					print('Deleted: %s'%current_file)
My Keybase Page: https://keybase.io/benjamichaelson Follow Me!