Spaces:
Running
Running
prasanth.thangavel
commited on
Commit
·
56831c4
1
Parent(s):
aa9b780
Add a helper util to rename the audio files properly
Browse files- utils.py/rename_files.py +32 -0
utils.py/rename_files.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import re
|
3 |
+
|
4 |
+
def rename_audio_files():
|
5 |
+
print (f"Current working directory: {os.getcwd()}")
|
6 |
+
|
7 |
+
# Get the audio directory path
|
8 |
+
audio_dir = 'static/audio'
|
9 |
+
print (f"audio_dir: {audio_dir}")
|
10 |
+
filenames = os.listdir(audio_dir)
|
11 |
+
print (f"# of files: {len(filenames)}")
|
12 |
+
|
13 |
+
# Loop through all files in the directory
|
14 |
+
for filename in filenames:
|
15 |
+
if filename.endswith('.mp3'):
|
16 |
+
# Create old and new file paths
|
17 |
+
old_path = os.path.join(audio_dir, filename)
|
18 |
+
new_filename = filename.replace('-', ' ')
|
19 |
+
new_filename = new_filename.replace('_', ' ')
|
20 |
+
new_filename = re.sub(r'\s+', ' ', new_filename)
|
21 |
+
new_path = os.path.join(audio_dir, new_filename)
|
22 |
+
|
23 |
+
# Rename the file
|
24 |
+
try:
|
25 |
+
os.rename(old_path, new_path)
|
26 |
+
print(f'Renamed: {filename} -> {new_filename}')
|
27 |
+
except OSError as e:
|
28 |
+
print(f'Error renaming {filename}: {e}')
|
29 |
+
|
30 |
+
if __name__ == '__main__':
|
31 |
+
rename_audio_files()
|
32 |
+
|