Skip to content

Instantly share code, notes, and snippets.

@James-Firth
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save James-Firth/9572933 to your computer and use it in GitHub Desktop.
Save James-Firth/9572933 to your computer and use it in GitHub Desktop.
Rename a folder of tv show files.
#############################################################################################
# The MIT License (MIT)
# Copyright (c) 2014 James Firth
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#####################################################################################
#To run: python tv_renamer.py <FULL PATH> <SHOWNAME> <SEASON #> <first episode # in folder>(Optional)
#The last param is in case you have multiple discs for a single season and rip each disc to a separate folder.
#This renames all the items in <FULL PATH> with the format: <SHOWNAME>.s<SEASONNUMBER>.eXX.extension
#The script assumes the items are already ordered within the folder however.
import os
import sys
from glob import glob
def getExtension(string):
array = string.split(".")
return array[-1]
def rename(dirname,showname,season,firstEpInFolder):
#makes sure it's a directory
if os.path.isdir(dirname):
episodeNum = int(firstEpInFolder)
print("Renaming...")
#loop through directory
for fname in os.listdir(dirname):
fpath = os.path.join(dirname,fname)
print("Checking..."+fpath)
if os.path.isfile(fpath):
#create the new name
newpath = os.path.join(dirname,showname+"."+"s"+season.zfill(2)+"e"+str(episodeNum).zfill(2)+"."+getExtension(fname))
#move/rename the file
os.rename(fpath,newpath)
print("Renamed '%s' to '%s'.") % (fpath,newpath)
episodeNum = episodeNum +1
else:
print("'"+fpath+"' is not a file.")
else:
print("ERROR: Not a directory")
def main():
if len(sys.argv) >= 4:
temp_dirname = glob(sys.argv[1])
showname = sys.argv[2]
season = sys.argv[3]
if(len(sys.argv) >= 5):
firstEpNum = sys.argv[4]
else:
firstEpNum = 1
if len(temp_dirname) >= 1:
dirname = temp_dirname[0]
if os.path.isdir(dirname):
rename(dirname,showname,season,firstEpNum)
else:
print("Error, improper pathname.")
else:
print("Error: Not enough arguments")
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment