import os import sys def rename_files(directory): for root, dirs, files in os.walk(directory): for filename in files: if '_2F' in filename: new_filename = filename.replace('_2F', '%2F') original_path = os.path.join(root, filename) new_path = os.path.join(root, new_filename) if not os.path.exists(new_path): # If the new filename doesn't exist, rename the file print(f"Renaming '{original_path}' to '{new_path}'") os.rename(original_path, new_path) else: # If the new filename exists, delete the original file print(f"Deleting '{original_path}' because '{new_filename}' already exists.") os.remove(original_path) else: continue if __name__ == '__main__': if len(sys.argv) != 2: print("Usage: python3 rename_script.py ") sys.exit(1) directory = sys.argv[1] if not os.path.isdir(directory): print(f"The provided path '{directory}' is not a directory.") sys.exit(1) rename_files(directory)