Skip to content

Instantly share code, notes, and snippets.

@tatterdemalion
Created November 23, 2014 22:26
Show Gist options
  • Save tatterdemalion/5d5e6dcfe70b827e2124 to your computer and use it in GitHub Desktop.
Save tatterdemalion/5d5e6dcfe70b827e2124 to your computer and use it in GitHub Desktop.
organize nikon raw files in an external drive
import os
import sys
import shutil
import filecmp
from datetime import datetime
def get_outpath(path, to):
timestamp = os.path.getctime(path)
created = datetime.fromtimestamp(timestamp)
directory = os.path.join(
to, str(created.year), str(created.month), str(created.day))
if not os.path.exists(directory):
os.makedirs(directory)
filename = str(timestamp) + '.NEF'
return os.path.join(directory, filename)
def copy(path, outpath, compare=False):
error = None
if os.path.exists(outpath):
state = 'already exists in'
if compare and not filecmp.cmp(path, outpath, shallow=True):
error = {'filename': path, 'outpath': outpath}
else:
shutil.copy2(path, outpath)
state = 'copied to'
return {'state': state, 'error': error}
if __name__ == '__main__':
if len(sys.argv) < 3:
print """
This small application help organizing raw Nikon images in an external
drive. Use it with caution.
--compare: compares existing files with current ones and returns an
error log at the end if they don't match.
example usage:
python neforganize.py <path> <external-drive-path> --compare"""
sys.exit(1)
cwd = sys.argv[1]
to = sys.argv[2]
compare = False
if len(sys.argv) > 3 and sys.argv[3] == '--compare':
compare = True
errors = []
counter = 0
for root, dirs, files in os.walk(cwd):
for filename in files:
if filename.endswith('.NEF'):
path = os.path.join(root, filename)
outpath = get_outpath(path, to)
result = copy(path, outpath, compare)
counter += 1
if result.get('error'):
errors.append(result.get('error'))
print('%s - %s %s %s' % (
counter, filename, result.get('state'), outpath))
if errors:
print("ERRORS:")
print(errors)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment