Skip to content

Instantly share code, notes, and snippets.

@nkrh
Created January 27, 2018 02:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkrh/8ba296eb03e578ee7210c1cade4f9d00 to your computer and use it in GitHub Desktop.
Save nkrh/8ba296eb03e578ee7210c1cade4f9d00 to your computer and use it in GitHub Desktop.
Bulk rename with Python
import os, re
path = 'C:/Users/Test/test'
pattern = '^([A-Z]+)\-([0-9]+).*$'
replace = r"\2-\1.mkv"
comp = re.compile(pattern)
for f in os.listdir(path):
full_path = os.path.join(path, f)
if os.path.isfile(full_path):
match = comp.search(f)
if not match :
continue
try:
new_name = match.expand(replace)
new_name = os.path.join(path, new_name)
except re.error:
continue
if os.path.isfile(new_name):
print('%s -> %s skipped' % (f, new_name))
else:
os.rename(full_path, new_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment