Created
March 4, 2023 22:59
-
-
Save keithah/99b5178d3188860d7482a6169939323d to your computer and use it in GitHub Desktop.
dupe script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import shutil | |
import sys | |
def compare_and_delete_duplicates(dir1, dir2): | |
""" | |
Recursively compare files in dir1 and dir2, and delete duplicates in dir2. | |
""" | |
# Get a list of all files in dir1 and dir2 | |
dir1_files = [os.path.join(dir1, f) for f in os.listdir(dir1)] | |
dir2_files = [os.path.join(dir2, f) for f in os.listdir(dir2)] | |
# Check each file in dir2 against those in dir1 | |
for file2 in dir2_files: | |
if os.path.isfile(file2): | |
for file1 in dir1_files: | |
if os.path.isfile(file1): | |
if filecmp.cmp(file1, file2): | |
# If the files are the same, delete the one in dir2 | |
os.remove(file2) | |
print("Deleted duplicate file: ", file2) | |
break | |
elif os.path.isdir(file1): | |
# If the file in dir1 is a directory, recursively call this function on it | |
compare_and_delete_duplicates(file1, dir2) | |
if __name__ == '__main__': | |
if len(sys.argv) != 3: | |
print('Usage: python script.py <dir1> <dir2>') | |
else: | |
dir1 = sys.argv[1] | |
dir2 = sys.argv[2] | |
compare_and_delete_duplicates(dir1, dir2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment