Skip to content

Instantly share code, notes, and snippets.

@vasc
Last active December 29, 2015 04:19
Show Gist options
  • Save vasc/7613649 to your computer and use it in GitHub Desktop.
Save vasc/7613649 to your computer and use it in GitHub Desktop.
Clean filenames recursevely
import os
import os.path
import re
for dirpath, dirnames, filenames in os.walk('.'):
print dirpath
print
for dirname in dirnames: print dirname
print
for filename in filenames:
new_filename = filename
new_filename = re.sub(r'\s*-\s*', '-', new_filename)
new_filename = re.sub(r'-(\d\d)x(\d\d)-', r'.S\1E\2.', new_filename)
new_filename = re.sub(r'[\s\,\+]+', '.', new_filename)
new_filename = re.sub(r'\.+', '.', new_filename)
new_filename = re.sub(r'Season.(\d).Episode.(\d\d)', r'S0\1E\2', new_filename)
os.rename(os.path.join(dirpath, filename), os.path.join(dirpath, new_filename))
print os.path.join(dirpath, new_filename)
print
curl -o /tmp/clean_file_names.py "https://gist.github.com/vasc/7613649/raw/clean_file_names.py" 2> /dev/null && python /tmp/clean_file_names.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment