Last active
December 29, 2015 12:19
-
-
Save thomasjpfan/7669664 to your computer and use it in GitHub Desktop.
Command line util to organize photos in folders with the format: '{year}/{year} - {month} - {month_name}'
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 argparse | |
from datetime import datetime | |
from calendar import month_name as months | |
import shutil | |
EXTENSIONS = ['.jpg', '.png', '.mov', '.mp4', '.tif', '.jpeg', '.raw'] | |
OUTPUT_TEMPLATE = '/{year}/{year} - {month} - {month_name}' | |
def is_valid_directory(directory): | |
if os.path.isdir(directory): | |
return directory | |
else: | |
msg = "{} directory does not exist".format(directory) | |
raise argparse.ArgumentTypeError(msg) | |
parser = argparse.ArgumentParser( | |
description="Takes photos in the src's directory/subdirectories and copies \ | |
them into the output directory in folder structure: year/year-month-month_name") | |
parser.add_argument(dest='src', | |
metavar='src', | |
type=is_valid_directory, | |
nargs='?', | |
default='.', | |
help='directory of photos, defaults to working directory') | |
parser.add_argument(dest='output', | |
metavar='output', | |
nargs='?', | |
default='OUTPUT', | |
help='defaults to folder named OUTPUT in working directory') | |
parser.add_argument('-e', | |
dest='extra', | |
nargs='?', | |
default='EXTRA', | |
help=' '.join(['folder for files not'] + EXTENSIONS)) | |
parser.add_argument('-d', '--dry-run', | |
action='store_true', | |
help='dry run') | |
parser.add_argument('-mv', '--move', | |
action='store_true', | |
help='move files instead of copying') | |
args = parser.parse_args() | |
output = args.output | |
if output[-1] == '/': | |
output = output[:-1] | |
extra_dir = args.extra | |
if extra_dir[-1] == '/': | |
extra_dir = extra_dir[:-1] | |
output_template = ''.join([output, OUTPUT_TEMPLATE]) | |
extra_dir = ''.join([output, '/', extra_dir]) | |
dry_run = args.dry_run | |
move = args.move | |
for dirname, dirnames, filenames in os.walk(args.src): | |
for filename in filenames: | |
exten = os.path.splitext(filename)[-1].lower() | |
name = os.path.join(dirname, filename) | |
if exten == '.ds_store': | |
continue | |
elif exten in EXTENSIONS: | |
secs_since_epoch = os.stat(name).st_mtime | |
date = datetime.fromtimestamp(secs_since_epoch) | |
year, month, month_name = date.year, date.month, months[date.month] | |
output_dir = output_template.format(year=year, | |
month=month, | |
month_name=month_name) | |
else: | |
output_dir = extra_dir | |
if not os.path.isdir(output_dir) and not dry_run: | |
os.makedirs(output_dir) | |
output_path = os.path.join(output_dir, filename) | |
print '{} -> {}'.format(filename, output_dir) | |
if not dry_run: | |
if move: | |
shutil.move(name, output_path) | |
else: | |
shutil.copy2(name, output_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment