文章目录

最近整理女儿的照片,想按日期对照片进行分类,以便看看女儿每个阶段的成长

网上一搜 找到了PhotoTool和Adebis_Photo_Sorter支持按日期分类整理

可惜PhotoTool直接crash,Adebis_Photo_Sorter倒是ok就是不支持视频的

好吧,只有自己动手写一个咯

有需要的可以自己动手改改立马就能跑起来哦

PS: 我这里用modified time来分类的,一般默认情况下相机出来的照片EXIF里面的拍摄时间和modified time应该是一样的,

如果有需要也可以用pyexif来取EXIF信息

# coding:GBK

# !/usr/bin/python

# 11-10-8

import os  
import time

count = 0

def get\_file\_modify_time(pathname):  
t = os.path.getmtime(pathname)  
return time.strftime('%Y-%m-%d', time.localtime(t))

def copy\_file\_by_mtime(sourceFile, targetPath, filename):  
c = 1  
mtime = get\_file\_modify_time(sourceFile)  
targetPath = os.path.join(targetPath, mtime)  
targetFile = os.path.join(targetPath, filename)  
if not os.path.exists(targetPath):  
os.makedirs(targetPath)  
while True:  
if not os.path.exists(targetFile):  
open(targetFile, "wb").write(open(sourceFile, "rb").read())  
return  
else:  
targetFile = os.path.join(targetPath, "%d_%s" % (c, filename))  
c += 1

def dispatch_file(sourcePath, targetPath):  
global count  
for file in os.listdir(sourcePath):  
sourceFile = os.path.join(sourcePath, file)  
if os.path.isfile(sourceFile):  
count += 1  
print count  
copy\_file\_by_mtime(sourceFile, targetPath, file)  
if os.path.isdir(sourceFile):  
dispatch_file(sourceFile, targetPath)

dispatch_file("H:/喵喵", "H:/喵喵成长记录")  
print "Copy Files :", count  
文章目录