Created
February 6, 2012 11:27
-
-
Save vadimii/1751615 to your computer and use it in GitHub Desktop.
Sort photo, video files
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
import os | |
import re | |
import shutil | |
import string | |
JPEG = re.compile(r'^img_\d{4}\.jpg$', re.I) | |
MOV = re.compile(r'^img_\d{4}\.mov$', re.I) | |
DATE = re.compile(r'^.*\d{4}-\d{2}-\d{2}$') | |
PHOTO_DST_DIR = './photo/' | |
VIDEO_DST_DIR = './video/' | |
ROOT = '..' | |
def get_dst(src, dst_root): | |
fname = os.path.basename(src) | |
name, ext = os.path.splitext(fname) | |
date = os.path.dirname(src) | |
date = os.path.basename(date) | |
date = string.replace(date, '-', '_') | |
new_name = '{0}_{1}{2}'.format(date, name, ext) | |
return os.path.join(dst_root, new_name) | |
for entry in os.listdir(ROOT): | |
found_dir = os.path.join(ROOT, entry) | |
if not (os.path.isdir(found_dir) and re.match(DATE, entry)): | |
continue | |
for f in os.listdir(found_dir): | |
src = os.path.join(found_dir, f) | |
if not os.path.isfile(src): | |
continue | |
if re.match(JPEG, f): | |
dst = get_dst(src, PHOTO_DST_DIR) | |
print dst | |
#shutil.copy2(src, dst) | |
elif re.match(MOV, f): | |
dst = get_dst(src, VIDEO_DST_DIR) | |
print dst | |
#shutil.copy2(src, dst) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment