Python script: rename.py
Posted 3 years, 4 months ago at 2:20 pm. 4 comments
I like to have my music, movie, and picture files named a certain way. When I download files from the internet, they usually don’t follow my naming convention. I found myself manually renaming each file to fit my style. This got old realy fast, so I decided to write a program to do it for me.
This program can convert the filename to all lowercase, replace strings in the filename with whatever you want, and trim any number of characters from the front or back of the filename. Here is the usage output:
usage: rename.py [options] file1 ... fileN options: -h, --help show this help message and exit -v, --verbose Use verbose output -l, --lowercase Convert the filename to lowercase -fNUM, --trim-front=NUM Trims NUM of characters from the front of the filename -bNUM, --trim-back=NUM Trims NUM of characters from the back of the filename -rOLDVAL NEWVAL, --replace=OLDVAL NEWVAL Replaces OLDVAL with NEWVAL in the filename
Here is a few examples of what this program can do.
]$ ls -l total 0 -rw-r--r-- 1 matt matt 0 Mar 4 14:03 01-BandName_-_SongName-group.mp3 -rw-r--r-- 1 matt matt 0 Mar 4 14:03 02-BandName_-_SongName2-group.mp3 -rw-r--r-- 1 matt matt 0 Mar 4 14:03 03-BandName_-_SongName3-group.mp3 ]$ rename.py -f3 -r "_-_" "-" -r "-group" "" *.mp3 ]$ ls -l total 0 -rw-r--r-- 1 matt matt 0 Mar 4 14:03 BandName-SongName.mp3 -rw-r--r-- 1 matt matt 0 Mar 4 14:03 BandName-SongName2.mp3 -rw-r--r-- 1 matt matt 0 Mar 4 14:03 BandName-SongName3.mp3 ]$ rename.py --replace="Band" "" -lv *.mp3 BandName-SongName.mp3 -> name-songname.mp3 BandName-SongName2.mp3 -> name-songname2.mp3 BandName-SongName3.mp3 -> name-songname3.mp3 ]$ ls -l total 0 -rw-r--r-- 1 matt matt 0 Mar 4 14:03 name-songname.mp3 -rw-r--r-- 1 matt matt 0 Mar 4 14:03 name-songname2.mp3 -rw-r--r-- 1 matt matt 0 Mar 4 14:03 name-songname3.mp3




Hey!
I have the very same problem! So I googled it, and found this. But I don’t know how to use it?
Can you please explain further?
Tried to import rename.py and the use the funktion: RenameFile(-r3 -r “_-_” “-”, filepath here)
But that didn’t work..
Hello Matt,
Thanks for the great script.
I hear that print has been replaced with a print function in python 3. I was too lazy to update the prints in your script so just commented them out.
Worked like a charm.
Great script, worked like a charm renaming the first few letters of 100 folders for me in one second.
python rename.py -f11 -v *
Any advice for using this script in Windows (Vista 64 SP2)? I get an error when I try to use wildcard characters, even if I specify the file path. Ideas appreciated.
F:\Workspace\website\test>python rename.py -f10 -r “_County_300dpi_Binary_Georef
d” “HistoricCountyHighwayMapScan_wvsrc_1937_utm83″ f:/workspace/website/test/WVD
OH*.rrd
Error renaming ‘f:/workspace/website/test/WVDOH*.rrd’: The filename, directory n
ame, or volume label syntax is incorrect