File size: 1,059 Bytes
56831c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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()