Skip to content

Instantly share code, notes, and snippets.

@costa86
Created October 28, 2023 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save costa86/b8728ad26bee0bde27b677357987ad28 to your computer and use it in GitHub Desktop.
Save costa86/b8728ad26bee0bde27b677357987ad28 to your computer and use it in GitHub Desktop.
Remove duplicated files in a folder
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