Last active
May 17, 2021 10:37
-
-
Save TheFrostlixen/08959471726cd27f559c596c89e9ff5a to your computer and use it in GitHub Desktop.
Python script to batch rename files via regex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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