Created
January 27, 2018 02:51
-
-
Save nkrh/8ba296eb03e578ee7210c1cade4f9d00 to your computer and use it in GitHub Desktop.
Bulk rename with Python
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 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