#!/usr/bin/python """ 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 - " __version__ = "$Revision: 0.1 $" __copyright__ = "Copyright (c) 2009 Alessio Sclocco" __license__ = "GNU General Public License version 3" import os import sys import getopt import string def usage(): print __doc__ # main program 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 = string.upper(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) 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]) if fileName[0] == "/": old = fileName else: old = dir + "/" + fileName new = dir + "/" + newFileName os.rename(old, new) episode = episode + 1 sys.exit(0)