Created
November 4, 2013 07:38
-
-
Save honzajavorek/7299289 to your computer and use it in GitHub Desktop.
Fix filenames
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
#!/usr/bin/env python | |
import os | |
import re | |
from unidecode import unidecode | |
if __name__ == '__main__': | |
folder = os.getcwd() | |
for filename in os.listdir(folder): | |
original_path = os.path.join(folder, filename) | |
name, ext = os.path.splitext(filename) | |
new_name = unidecode(name.decode('utf-8')).lower() | |
new_name = re.sub(r'\W+', '-', new_name).strip('-') | |
new_name = new_name + ext.lower() | |
new_path = os.path.join(folder, new_name) | |
if not os.path.exists(new_path): | |
os.rename(original_path, new_path) | |
else: | |
print 'Already exists: ' + new_name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment