Last active
December 4, 2015 13:50
-
-
Save hbldh/58d837dfe19e1999076c to your computer and use it in GitHub Desktop.
A simple program for rearranging files, e.g. photos in a photo collection.
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 | |
# -*- coding: utf-8 -*- | |
""" | |
move_photos.py | |
================== | |
Created by: hbldh <henrik.blidh@nedomkull.com> | |
Created on: 2015-12-03, 22:34 | |
""" | |
from __future__ import division | |
from __future__ import print_function | |
from __future__ import unicode_literals | |
from __future__ import absolute_import | |
import os | |
import re | |
import shutil | |
def move_photos(source_path, destination_path=None): | |
source_path = os.path.abspath(os.path.expanduser(source_path)) | |
if destination_path is None: | |
destination_path = source_path | |
else: | |
destination_path = os.path.abspath(os.path.expanduser(destination_path)) | |
# Use os.walk to iterate from one folder through all its subfolders. | |
for path, dirs, files in os.walk(source_path): | |
# Use regexp to see if folder name matches pattern yyyy_mm_dd. | |
date_match = re.search('([0-9]{4})_([0-9]{2})_([0-9]{2})', os.path.basename(path)) | |
# If match is None, then this folder is not interesting. | |
# Else, read the matched numbers into separate variables. | |
if date_match is None: | |
print("Skipping [{0}]...".format(path)) | |
continue | |
else: | |
print("Handling [{0}]!".format(path)) | |
year, month, day = date_match.groups() | |
# Create the destination path for this date's photos. | |
# This folder is destination_path + '/yyyy/mm/'. | |
dest_path = os.path.join(destination_path, year, month) | |
# If these folders do not exists, create them. | |
if not os.path.exists(dest_path): | |
os.makedirs(dest_path) | |
# Move copy each file in the source folder to the destination path folder. | |
for f in files: | |
file_source = os.path.join(path, f) | |
file_dest = os.path.join(dest_path, f) | |
print("Moving [{0}] => [{1}]...".format(file_source, file_dest)) | |
shutil.move(file_source, file_dest) | |
# Try to remove the yyyy_mm_dd source folder. | |
# If it by some strange coincidence still contains something, an error | |
# wil be raised and we will alert the user. | |
try: | |
os.rmdir(path) | |
except OSError: | |
print("Folder {0} was not empty!".format(path)) | |
def main(): | |
import argparse | |
parser = argparse.ArgumentParser( | |
description='A tool for moving photos from "one folder per day" to "one folder per month" sorting.') | |
parser.add_argument('source_path', action='store', | |
help='The path of the folder containing "one folder per day" folders.') | |
parser.add_argument('--destination_path', action='store', default=None, required=False, | |
help='The path of the folder to house "one folder per month" tree. ' + | |
'If omitted, the source path will be the destination path as well.') | |
args = parser.parse_args() | |
move_photos(args.source_path, args.destination_path) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment