Created
October 28, 2023 11:22
-
-
Save costa86/b8728ad26bee0bde27b677357987ad28 to your computer and use it in GitHub Desktop.
Remove duplicated files in a folder
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 sys | |
def get_folder() -> str: | |
args = sys.argv | |
if len(args) != 2 or not os.path.isdir(args[1]): | |
print("a folder must be set as the first argument") | |
sys.exit(1) | |
return sys.argv[1] | |
def is_copy(file: os.DirEntry) -> bool: | |
return file.is_file() and not file.name.startswith("copy") and " copy" in file.name | |
def delete_confirmation(files: list[str]) -> bool: | |
if not files: | |
print("No duplicated files found") | |
return False | |
print(f"You are about to delete {len(files)} files:") | |
for i in files: | |
print(i) | |
delete_confirmation = input("Confirm? [y/Y for 'yes']") | |
return delete_confirmation.lower() == "y" | |
folder = os.scandir(get_folder()) | |
files = [i.path for i in folder if is_copy(i)] | |
if delete_confirmation(files): | |
for i in files: | |
os.remove(i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment