I am trying to rename a lot of music files. The files contain spaces and capitals and whatnot and I want to replace the spaces with underscores and remove all capital letters. Is there a way to do that? I have seen changing extensions and multiple file names that are almost identical save for a few letters or numbers changed but nothing like what I need.
1 Answer
A small python script will do the renaming recursively:
#!/usr/bin/env python3
import os
import shutil
import sys
directory = sys.argv[1]
for root, dirs, files in os.walk(directory):
for f in files:
new_f = root+"/"+f.replace(" ", "_").lower()
f = root+"/"+f
if f != new_f:
shutil.move(f, new_f)
where:
lower()
will lower all possible capitals .replace(" ", "_")
will replace spaces by underscores.if f != new_f
will only rename the files if it is actually needed.shutil.move(f, new_f)
will actually rename the file.
How to use
- Copy the script into an empty file
- Save it as
rename.py
Run it with the targeted directory as argument:
python3 /path/to/rename.py <directory>
Notes
- Use quotes if the targeted directory includes spaces.
- Mind that you will run into name clashes if the renamed file name already exists.
In case of possible name clashes
(If you already started renaming manually for example) Use the version below.
#!/usr/bin/env python3
import os
import shutil
import sys
directory = sys.argv[1]
for root, dirs, files in os.walk(directory):
for f in files:
renamed = f.replace(" ", "_").lower() ;new_f = root+"/"+renamed
old_f = root+"/"+f
if old_f != new_f:
n = 1
while os.path.exists(new_f):
new_f = root+"/dupe_"+str(n)+"_"+renamed
n = n+1
shutil.move(old_f, new_f)
It will renane:
into:
EDIT
The script above will rename files. From a comment, I understand you'd like to rename the folders likewise. With just a few lines, that can be done:
#!/usr/bin/env python3
import os
import shutil
import sys
directory = sys.argv[1]
def name_edit(item, root):
renamed = item.replace(" ", "_").lower() ;new_item = root+"/"+renamed
old_item = root+"/"+item
if old_item != new_item:
n = 1
while os.path.exists(new_item):
new_item = root+"/dupe_"+str(n)+"_"+renamed
n = n+1
shutil.move(old_item, new_item)
for root, dirs, files in os.walk(directory):
for item in files:
name_edit(item, root)
for item in dirs:
name_edit(item, root)
Explanation
In the second script, the way files were renamed is converted to a function, to prevent writing the same code twice (running it on both files/folders). Subsequently, the script first renames the files, then the folders recursively:
#!/usr/bin/env python3
import os
import shutil
import sys
directory = sys.argv[1]
def name_edit(item, root):
# in one command, both replace spaces and lower {possible} capitals
renamed = item.replace(" ", "_").lower()
# combine directory and (new) file- or folder name
new_item = root+"/"+renamed
# combine directory and (old) file- or folder name
old_item = root+"/"+item
# if the name was changed, check for possible existing dupes
# and rename until the name is unique
if old_item != new_item:
n = 1
while os.path.exists(new_item):
new_item = root+"/dupe_"+str(n)+"_"+renamed
n = n+1
# if the file or folder name was changed, apply the change
shutil.move(old_item, new_item)
for root, dirs, files in os.walk(directory):
# use os.walk() to find files and folders recursively
for item in files:
name_edit(item, root)
for item in dirs:
name_edit(item, root)
-
-
actually it may have but it didn't do the directories like i was hoping Nov 7, 2015 at 10:17
-
@RonDeeds ?? Could you explain? How doesn't it do the directories? The example in the image is a sub directory of the argument.
[edit]
tested it again, all sub and sub-sub directories are renamed correctly in my tests. Please let me know if- and what goes wrong. Nov 7, 2015 at 10:30 -
It changed all of the files in the main directory but not the subdirectories the files were in. I.e. it changed the mp3 files in '3 Doors Down' subdirectory in the 'Downloads' directory, but not the '3 Doors Down' subdirectory itself. I don't know if that makes sense or not. But I was hoping it would change the name of the subdirectory as well as the files within the subdirectory if that helps my explanation at all. Nov 7, 2015 at 21:50
-
You mentioned Can you change multiple dissimilar file names?, I deliberately left alone the folder (directory-) names. Will edit the answer probably tomorrow. Nov 7, 2015 at 21:57