Created
February 10, 2024 09:22
-
-
Save vpnry/a27253a55b63d9bfc90c23a68bf534f3 to your computer and use it in GitHub Desktop.
To rename files according to its parent dir name. Directory names are URL encoded
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
| # rename files according to its parent dir name | |
| # directory names are URL encoded | |
| # generated with CHATGPT (tested) | |
| import os | |
| from urllib.parse import unquote | |
| # Function to decode URL encoded folder names | |
| def decode_folder_name(folder_name): | |
| return unquote(folder_name) | |
| # Function to rename mp3 files according to folder name | |
| def rename_mp3_files(directory): | |
| for root, dirs, files in os.walk(directory): | |
| for file in files: | |
| print(file) | |
| if file.endswith(".mp3"): | |
| folder_name = os.path.basename(root) | |
| decoded_folder_name = decode_folder_name(folder_name) | |
| old_path = os.path.join(root, file) | |
| new_name = f"{decoded_folder_name}.mp3" | |
| new_path = os.path.join(root, new_name) | |
| os.rename(old_path, new_path) | |
| print(f"Renamed {file} to {new_name}") | |
| if __name__ == "__main__": | |
| # Specify the directory containing URL encoded folders | |
| directory_path = "path/to/directory" | |
| # Rename mp3 files in the directory | |
| rename_mp3_files(directory_path) | |
| print("Done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment