Skip to content

Instantly share code, notes, and snippets.

@lunalawl
Last active June 26, 2023 03:47
Show Gist options
  • Save lunalawl/23fafb2c923917bdb7b170687c0ac500 to your computer and use it in GitHub Desktop.
Save lunalawl/23fafb2c923917bdb7b170687c0ac500 to your computer and use it in GitHub Desktop.
import os
def delete_files_except_extension(directory, extensions):
for root, dirs, files in os.walk(directory):
for file in files:
file_path = os.path.join(root, file)
file_extension = os.path.splitext(file_path)[1]
if file_extension not in extensions:
try:
print(f"Deleting file: {file_path}")
os.remove(file_path)
except OSError as e:
print(f"Error occurred while deleting {file_path}: {e}")
# Prompt for directory path
directory_path = input("This will delete all files in the current directory except Harmonix rnd archives.\nExercise caution! I am not responsible if you accidentally delete something important!\n\nPress 'c' to continue or specify a path.\n\n")
if directory_path.lower() == 'c':
directory_path = os.getcwd() # Set the current directory
# Extensions to keep
extensions_to_keep = [".milo", ".milo_ps2", ".milo_xbox", ".milo_ps3", ".milo_wii", ".rnd", ".rnd_ps2", ".rnd_gc", ".rnd_xbox", ".gh", ".kr", ".py"]
# Call the function to delete files except the specified extensions
delete_files_except_extension(directory_path, extensions_to_keep)
# Display completion message
input("\nComplete!")
@jnackmclain
Copy link

lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment