Skip to content

Instantly share code, notes, and snippets.

@TheFrostlixen
Last active May 17, 2021 10:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save TheFrostlixen/08959471726cd27f559c596c89e9ff5a to your computer and use it in GitHub Desktop.
Save TheFrostlixen/08959471726cd27f559c596c89e9ff5a to your computer and use it in GitHub Desktop.
Python script to batch rename files via regex
import glob, os
# point path to directory containing the shows
# load episode titles into `ep.txt`
path = r"I:\Videos\show"
new_filenames = []
with open(path + '\\ep.txt', 'r') as f:
for line in f:
new_filenames.append(line)
fileglob = glob.glob(path + '\\*')
fileglob.remove(path + '\\ep.txt')
for i in range(len(fileglob)):
ext = os.path.splitext(fileglob[i])[1]
new_name = path + '\\' + new_filenames[i].strip() + ext
print(fileglob[i] + ' -> ' + new_name)
try:
os.rename(fileglob[i], new_name)
except:
continue
import glob, re, os
path = r"D:\Downloads\new\new"
for filename in glob.glob( path + '\\*' ):
pattern = r'\.'
replace = r' '
spaces_name = re.sub(pattern, replace, filename)
pattern = r'(.*) (19|20)(\d{2}).* (.{3})$' # movie
replace = r'\1 (\2\3).\4' # movie
#pattern = r'(.*) [sS](\d\d)[eE](\d\d).* (.{3})' # tv show
#replace = r'\1 [\2x\3].\4' # tv show
new_name = re.sub(pattern, replace, spaces_name)
print(filename + ' -> ' + new_name)
os.rename(filename, new_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment