Skip to content

Instantly share code, notes, and snippets.

@mikepietruszka
Created April 19, 2020 18:12
Show Gist options
  • Save mikepietruszka/09c03bc1ea3f8a4a8959168f86618fee to your computer and use it in GitHub Desktop.
Save mikepietruszka/09c03bc1ea3f8a4a8959168f86618fee to your computer and use it in GitHub Desktop.
Simple movie sorter
#!/usr/bin/env python
'''
Movie sorter
'''
import os
import shutil
import sys
path = sys.argv[1]
letters = []
for dir in next(os.walk(path))[1]:
if dir.startswith("the ".lower()):
temp_dir = dir.split(" ")
first_char = temp_dir[1][0]
elif dir.startswith("the_".lower()):
temp_dir = dir.replace("_", " ").lower().split(" ")
first_char = temp_dir[1][0]
else:
first_char = dir[0]
if first_char not in letters:
letters.append(first_char)
new_dir_path = os.path.join(path, first_char.upper())
if not os.path.exists(new_dir_path):
print("INFO: Making directory: {0}".format(
new_dir_path))
os.mkdir(new_dir_path)
else:
new_dir_path = os.path.join(path, first_char.upper())
print("Moving {0}{1} to {2}".format(path, dir, new_dir_path))
try:
shutil.move(os.path.join(path, dir), new_dir_path)
except Exception as err:
print("ERROR: {0}".format(str(err)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment