Skip to content

Instantly share code, notes, and snippets.

@anthonycastelli
Last active August 29, 2015 14:14
Show Gist options
  • Save anthonycastelli/26404c0ae4b3d4b5de11 to your computer and use it in GitHub Desktop.
Save anthonycastelli/26404c0ae4b3d4b5de11 to your computer and use it in GitHub Desktop.
Batch rename movie files
# To run this script, simply call
# $ python renamer.py "/path/to/folder" "S01" " - "
# This will take a file with a divider in it and add
# the appropriate formatting to work better with
# Plex Media Server.
# i.e. "Season 1 Episode 21 Episode Name.mp4" would become
# "S01E21 Episode Name.mp4"
import os
import sys
if len(sys.argv) < 3:
print('You must specifiy a folder and a season')
sys.exit(1)
folder = sys.argv[1]
season = sys.argv[2]
divider = ""
if sys.argv[3]:
divider = sys.argv[3]
renamed = False
os.chdir(folder)
for (index, file) in enumerate(os.listdir(folder)):
if file.lower().endswith((".avi", ".mp4", ".m4v", ".mkv")):
if divider in file:
start = file.index(divider) + len(divider)
end = len(file)
fileName = file[start:end]
newFileName = season + "E" + str(index + 1) + " " + fileName
os.rename(file, newFileName)
renamed = True
if renamed:
print("Renamed %s files in %s" % (len(os.listdir(folder)), folder))
else:
print("There were no files that needed renaming")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment