music-player / utils.py /rename_files.py
prasanth.thangavel
Add a helper util to rename the audio files properly
56831c4
import os
import re
def rename_audio_files():
print (f"Current working directory: {os.getcwd()}")
# Get the audio directory path
audio_dir = 'static/audio'
print (f"audio_dir: {audio_dir}")
filenames = os.listdir(audio_dir)
print (f"# of files: {len(filenames)}")
# Loop through all files in the directory
for filename in filenames:
if filename.endswith('.mp3'):
# Create old and new file paths
old_path = os.path.join(audio_dir, filename)
new_filename = filename.replace('-', ' ')
new_filename = new_filename.replace('_', ' ')
new_filename = re.sub(r'\s+', ' ', new_filename)
new_path = os.path.join(audio_dir, new_filename)
# Rename the file
try:
os.rename(old_path, new_path)
print(f'Renamed: {filename} -> {new_filename}')
except OSError as e:
print(f'Error renaming {filename}: {e}')
if __name__ == '__main__':
rename_audio_files()