All pastes #2007512 Raw Edit

XBMC TV Show Renamer

public python v1 · immutable
#2007512 ·published 2010-12-01 08:27 UTC
rendered paste body
#! /usr/bin/pythonimport osexts = [".264", ".mkv", ".mp4", ".mpeg", ".mpg", ".mov", ".avi", ".wmv", ".idx", ".sub", ".srt"] #list of valid file extensionssplitdir = os.getcwd().split("\\") #return array of the current working director, split by foldersnameinfo = splitdir[-2:] #make new array with last two elements of splitdir (Show name, season number)if int(nameinfo[1]) < 10 and len(nameinfo[1]) < 2: #if the folder number is from 1-9	start = nameinfo[0]+".S"+nameinfo[1].zfill(2)+"E" #make a variable and fill with <showName>.S<seasonNumber>.E"else: #anything higher than 10, or zero padded	start = nameinfo[0]+".S"+nameinfo[1]+"E"end = ".HDTV.XviD-2HD" #release type tagdef rename():	loops = 1	for file in os.listdir("."):		if file == "rename.py":			pass		elif os.path.isdir(file):			pass		else:			extension = os.path.splitext(file)[1] #get file extension			if extension in exts: #check it against the list of accepted extensions				if loops < 10: #check if the episode number will need zero padding					newname = start+str(loops).zfill(2)+end #create the new name from previously collected info					os.rename(file, newname+extension) #rename it add the file extension back on				elif os.path.isfile(file):					newname = start+str(loops)+end					os.rename(file, newname+extension)				loops+=1def check():	loops = 1	for file in os.listdir("."):		if file == "rename.py":			pass		elif os.path.isdir(file):			pass		else:			extension = os.path.splitext(file)[1]			if extension in exts:				if loops < 10:					newname = start+str(loops).zfill(2)+end					print "Renaming %s to %s" % (file, newname+extension)				elif os.path.isfile(file):					newname = start+str(loops)+end					print "Renaming %s to %s" % (file, newname+extension)				loops+=1check()print "Continue?: y/n"answer = raw_input()if answer == "y":	rename()else:	print "Exiting"