Skip to content

Instantly share code, notes, and snippets.

@vpnry
Created February 10, 2024 09:22
Show Gist options
  • Select an option

  • Save vpnry/a27253a55b63d9bfc90c23a68bf534f3 to your computer and use it in GitHub Desktop.

Select an option

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
# 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