I have a lot of passions, between them computer science and tv-shows.
I wanted to learn a bit of Python before the holidays so I’ve developed a simple (really simple) script in Python to change the filenames of my favorite tv-shows on the format that I prefer to archive them, something like “[LANG] TV-SHOW NAME – SEASONxEPISODE.ext“.
I’m not sure the script can be useful for someone else, but I like to share my code. The script can be downloaded here and can be read in the following part of the post.
"""
TV-Shows name changer
The script will change the filename of all the files in a certain dir to something like:
[LANG] TV-SHOW NAME - SEASONxEPISODE.ext
Usage: python showsNameChanger.py [options] [files]
Options:
-l ..., --language=... the language of the show
-n ..., --name=... the name of the show
-s ..., --season=... the season
-e ..., --episode=... the first episode of the list
-d ..., --dir=... the directory where the files are (if not provided, the current directory is used)
Files:
If a user doesn't want to change all the files in a dir, it is possible to list all the files to rename on the command line.
Assumptions:
1) The script will number all the files sequentially starting from the given episode number.
2) The files must be alphabetically sortable.
"""
__author__ = "Alessio Sclocco - <alessio@sclocco.eu>"
__version__ = "$Revision: 0.1 $"
__copyright__ = "Copyright (c) 2009 Alessio Sclocco"
__license__ = "GNU General Public License version 3"
import os
import sys
import getopt
def usage():
print __doc__
try:
params, args = getopt.getopt(sys.argv[1:], "l:n:s:e:d:", ["language=", "name=", "season=", "episode=", "dir="])
except getopt.GetoptError:
usage()
sys.exit(-1)
if not params:
usage()
sys.exit(0)
lang = ""
show = ""
season = ""
episode = 1
dir = "."
fileList = []
for opt, arg in params:
if opt in ("-l", "--language"):
lang = arg
elif opt in ("-n", "--name"):
show = arg
elif opt in ("-s", "--season"):
season = arg
elif opt in ("-e", "--episode"):
episode = int(arg)
elif opt in ("-d", "--dir"):
dir = arg
if not args:
fileList = os.listdir(dir)
fileList.sort()
else:
for file in args:
fileList.append(file)
fileList.sort()
for fileName in fileList:
if fileName[0] == ".":
continue
else:
newFileName = "[%s] %s - %sx%02d%s" % (lang, show, season, episode, os.path.splitext(fileName)[1])
os.rename(dir + "/" + fileName, dir + "/" + newFileName)
episode = episode + 1
sys.exit(0)

