Created
March 15, 2021 22:14
-
-
Save sirodoht/021acff077a118b3f113fe0791bc68c4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
from send2trash import send2trash | |
# get origin dir | |
photos_path = r"/Volumes/Leslie/G_processing/alltakeouts" | |
# index all origin files to a dict[filename] = path | |
log_filename = "json-files-to-delete.log" | |
log_file = open(log_filename, "w") | |
filename_to_path = {} | |
files_to_delete = [] | |
for dirpath, dirnames, filenames in os.walk(photos_path): | |
for filename in filenames: | |
if not filename.startswith(".") and filename[-4:] == "json": | |
files_to_delete.append(os.path.join(dirpath, filename)) | |
log_file.write(f"{dirpath}/{filename}\n") | |
print(f"wrote to {log_filename}") | |
print("lines count:", len(files_to_delete)) | |
#for filename in filename_to_path: | |
# print(f"{filename}: {filename_to_path[filename]}") | |
# Delete files | |
print("If you want to delete all the files please enter 'DELETE ALL'") | |
if input() == "DELETE ALL": | |
log_file = open("delete-record.log", "w") | |
for file in files_to_delete: | |
print(f"now deleting {file}") | |
#os.remove(file) # permenantly deletes files | |
send2trash(file) # sends files to trash | |
log_file.write(f"Deleted {file}\n") | |
print("wrote delete-record.log") | |
log_file.write(f"\nAll {len(filename_to_path)} files deleted") | |
log_file.close() | |
print(f"\n\nAll {len(filename_to_path)} files deleted") | |
else: | |
print("No files deleted") |
This file contains hidden or 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 hashlib | |
import os | |
from send2trash import send2trash | |
# get origin dir | |
origin_path = r"/Volumes/Leslie/G_processing/alltakeouts/albums" | |
# get desitnation dir | |
destination_path = r"/Volumes/Leslie/G_processing/alltakeouts/dates" | |
origin_log = open("origin.log", "w") | |
origin_files_dict = {} | |
origin_count = 0 | |
for dirpath, dirnames, filenames in os.walk(origin_path): | |
for filename in filenames: | |
if filename.startswith("."): | |
continue | |
fullpath = os.path.join(dirpath, filename) | |
print(f"{origin_count}: reading {fullpath}") | |
filedato = open(fullpath, "rb") | |
data = filedato.read() | |
hashed = hashlib.sha256(data).digest() | |
origin_files_dict[hashed] = fullpath | |
origin_log.write(f"{dirpath}/{filename}\n") | |
origin_count += 1 | |
print("origin files count:", len(origin_files_dict)) | |
print("origin files set count:", len(set(origin_files_dict))) | |
## crawl all destination dir files, if filename in dict add to list to delete | |
dest_log = open("destination.log", "w") | |
dest_files_dict = {} | |
dest_count = 0 | |
to_delete_log = open("to-delete.log", "w") | |
files_to_delete = [] | |
for dirpath, dirnames, filenames in os.walk(destination_path): | |
for filename in filenames: | |
if filename.startswith("."): | |
continue | |
fullpath = os.path.join(dirpath, filename) | |
print(f"{dest_count}: reading {fullpath}") | |
filedato = open(fullpath, "rb") | |
data = filedato.read() | |
hashed = hashlib.sha256(data).digest() | |
dest_files_dict[hashed] = fullpath | |
dest_log.write(f"{dirpath}/{filename}\n") | |
dest_count += 1 | |
if hashed in origin_files_dict: | |
files_to_delete.append(fullpath) | |
to_delete_log.write(fullpath + "\n") | |
print(f"Found {len(files_to_delete)} files") | |
print(f"Found {len(set(files_to_delete))} (set) files") | |
# Delete files | |
print("If you want to delete all the files please enter 'DELETE ALL'") | |
if input() == "DELETE ALL": | |
actually_deleted_log = open("actually-deleted.log", "w") | |
for file in files_to_delete: | |
# os.remove(file) # permenantly deletes files | |
send2trash(file) # sends files to trash | |
print(f"Deleted {file}") | |
actually_deleted_log.write(f"Deleted {file}\n") | |
actually_deleted_log.write(f"\nAll {len(files_to_delete)} files deleted") | |
actually_deleted_log.close() | |
print(f"\nAll {len(files_to_delete)} files deleted") | |
else: | |
print("No files deleted") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment