Tag Archives: renaming

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