Created
December 13, 2014 15:27
-
-
Save mos3abof/acd0766e9c16cdbb9160 to your computer and use it in GitHub Desktop.
Removes any non-alphanumeric characters, chacnges cases to lower cases and spaces to dots.
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/python | |
# -*- coding: utf-8 -*- | |
#@author Mosab Ahmad <mosab.ahmad@gmail.com> | |
import os | |
import re | |
MOVIES_PATH = '/home/mosab/Videos/youtube' | |
def proper_name(movie_name): | |
# replace any non alphanumeric character with a space | |
movie_name = re.sub(r'\W+', ' ', movie_name) | |
# replace spaces with dots :) | |
movie_name_list = movie_name.split(' ') | |
movie_name_list = filter(None, movie_name_list) | |
movie_name = '.'.join(e for e in movie_name_list).lower() | |
return movie_name | |
if __name__ == '__main__': | |
# go over the movies | |
for root, dirs, files in os.walk(MOVIES_PATH, topdown=False): | |
# rename files | |
for name in files: | |
old_file_name = os.path.join(root, name) | |
new_file_name = os.path.join(root, proper_name(name)) | |
os.rename(old_file_name, new_file_name) | |
# rename folders | |
for name in dirs: | |
old_dir_name = os.path.join(root, name) | |
new_dir_name = os.path.join(root, proper_name(name)) | |
os.rename(old_dir_name, new_dir_name) | |
# happy ending :) | |
print 'We are done!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment