Skip to content

Instantly share code, notes, and snippets.

@idiamant
Last active March 23, 2020 11:17
Show Gist options
  • Save idiamant/b5638745e69aedf2dc21b799c9b7b431 to your computer and use it in GitHub Desktop.
Save idiamant/b5638745e69aedf2dc21b799c9b7b431 to your computer and use it in GitHub Desktop.
Sample of how to delete files from dir1 that doesn't exist in dir2 - Answer to post: https://www.facebook.com/groups/mac.israel/permalink/2941748212604513/
#!/usr/bin/env python
import os
import sys
dir1_list = os.listdir('dir1') # Directory with many files
dir2_list = os.listdir('dir2') # Directory with duplicates of dir1 and maybe others
delete_list = []
for filename in dir1_list:
if filename in dir2_list:
#print ("File {} exists in both directories".format(filename))
pass
else:
#print ("File {} DOES NOT exist in both, will be deleted from dir1".format(filename))
delete_list.append(filename) # Deletes all files in dir1 that doesn't exist in dir2
for filename in delete_list:
os.remove('dir1/{}'.format(filename))
#!/usr/bin/env python
import os
import sys
os.mkdir("dir1")
os.mkdir("dir2")
for num in range(1000):
filename = "file-{}.txt".format(num)
with open('dir1/{}'.format(filename),'a') as f:
f.write("File content of {}".format(filename))
if (num % 5 == 0):
with open('dir2/{}'.format(filename),'a') as f:
f.write("File content of {}".format(filename))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment