Python script: rename.py

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

Files:
http://www.mattweber.org/files/rename.py

Leave a comment ?

12 Comments.

  1. 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..

  2. 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.
    :)

  3. Great script, worked like a charm renaming the first few letters of 100 folders for me in one second.
    python rename.py -f11 -v *

  4. 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:Workspacewebsitetest>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

  5. Does what it promises. Thanks for publishing that!

  6. Eric,

    It seems that wildcard characters are not recognized by os.path in Windows. To make it work, you can modify the rename.py file:

    After “import os”
    Add “import glob”

    Replace “for filename in args:”
    By “for filename in sum(map(glob.glob, args),[]):”

    I tried renaming a directory of pdfs. Works great!

  7. I have the same problem as Eric.
    My OS: Windows Vista.
    I can guess that it’s because I have spaces in foldernames and filenames, I tried to type full path of one file and get several errors each containing letters between spaces.

    Maybe I’m wrong.

    Any suggestions how I can use this script or modify it so that I will be able to use it?

  8. No, spaces in foldernames isn’t the problem. The problem is wildcard *

    ERROR “The filename, directory name, or volume label syntax is incorrect”

    [and by the way, how can I type several filenames (as mentioned in program usage) containing spaces??]

  9. I’ve just solved the problem.
    On DevShed forum some kind guy reccomended me to use glob. It worked for me so nice, I only modified 1 string and added 1 string.

    Thank you for great script, Matt!

    Paul >>>RAM>>>.

  10. Excellent, just took care of bunches of commas in about 7000 mp3 filenames. Thanks!

    It would be nice if it took care of directory names too. :)

  11. Thanx, I had a bunch of *srt files to rename, it’s perfect tool for the job.

  12. why don’t you use easytag?

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>