This script will help you to sort videos just use it as below and let him deal with it ;)
$ python ps-sorter.py "/path/to/course-directory"
""" | |
Plural Sight Courses sorter | |
@author: <[email protected]> Ahmed Ashraf | |
@twitter: @ahmedash95 | |
This script will help you to sort videos | |
just use it as below and let him deal with it ;) | |
``` | |
$ python ps-sorter.py "/path/to/course-directory" | |
``` | |
""" | |
import sys | |
from os import listdir,sep,rename | |
from os.path import isfile, join | |
args = sys.argv | |
# directory of videos | |
course_directory = args[1] | |
# List all video files inside this directory | |
files = [f for f in listdir(course_directory) if isfile(join(course_directory, f))] | |
def get_new_file_name(name): | |
return "-".join(name.split("-")[-2:]).split('.')[0] + " - " + name | |
def rename_file(path,old,new): | |
rename(path + sep + old,path + sep + new) | |
# Get new names for files | |
files_names = [] | |
for f in files: | |
new_name = get_new_file_name(f) | |
files_names.append([f,new_name]) | |
sorted_files_list = sorted(files_names, key=lambda k: k[1]) | |
# rename files | |
i = 1 | |
for old_name,new_name in sorted_files_list: | |
new_name_schema = "{0:02} - ".format(i) + new_name | |
rename_file(course_directory,old_name,new_name_schema) | |
print "Done For File : {}".format(new_name) | |
i+=1 | |
print "Your course files has been sorted succesfully" |