Created
March 17, 2019 01:13
-
-
Save jesusgoku/7588ed45c68fc6cb91922ae2a9e2eec9 to your computer and use it in GitHub Desktop.
Organize movies on folders by year
This file contains hidden or 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 | |
from __future__ import print_function | |
import os | |
import re | |
import sys | |
import shutil | |
import subprocess | |
def main(): | |
if (len(sys.argv) < 3): | |
print('Usage: {} ./source ./dest'.format(sys.argv[0])) | |
sys.exit(1) | |
source_folder = os.path.abspath(sys.argv[1]) | |
dest_folder = os.path.abspath(sys.argv[2]) | |
if (not os.path.exists(source_folder)): | |
print('Source folder not exists ({})'.format(source_folder)) | |
sys.exit(1) | |
if (not os.path.exists(dest_folder)): | |
print('Destination folder not exists ({})'.format(dest_folder)) | |
sys.exit(1) | |
if (not os.path.isdir(source_folder)): | |
print('Source folder is not a folder ({})'.format(source_folder)) | |
sys.exit(1) | |
if (not os.path.isdir(dest_folder)): | |
print('Destination folder is not a folder ({})'.format(dest_folder)) | |
sys.exit(1) | |
process_folder(source_folder, dest_folder) | |
def process_folder(source_folder, dest_folder): | |
files = os.listdir(source_folder) | |
for item in files: | |
full_path = os.path.join(source_folder, item) | |
if (os.path.isdir(full_path)): | |
process_folder(full_path, dest_folder) | |
else: | |
process_file(full_path, dest_folder) | |
def process_file(source_path, dest_folder): | |
print('\n\nProcess: {}'.format(source_path)) | |
matches = re.match(r'^(?:.*/)?.+\.(\d{4})\..+$', source_path) | |
if (not matches): | |
print(' --> Skip, not found movie year ({})'.format(source_path)) | |
return | |
year = matches.group(1) | |
year_folder = os.path.join(dest_folder, year) | |
dest_path = os.path.join(year_folder, os.path.basename(source_path)) | |
if (not os.path.exists(year_folder)): | |
os.mkdir(year_folder) | |
if (os.path.exists(dest_path)): | |
print(' --> Skip, destination path exists ({})'.format(dest_path)) | |
return | |
print(' --> Move {} --> {}'.format(source_path, dest_path)) | |
# os.rename(source_path, dest_path) | |
# os.system('mv {} {}'.format(source_path, dest_path)) | |
subprocess.call(['mv', source_path, dest_path]) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment