python 进行照片分类_按拍摄日期归类照片 一键搞定 Python

# -*- coding: gbk -*-

"""

功能:对照片按照拍摄时间进行归类

使用方法:将脚本和照片放于同一目录,双击运行脚本即可

作者:冰蓝

"""

import shutil

import os

import time

import exifread

class ReadFailException(Exception):

pass

def getOriginalDate(filename):

try:

fd = open(filename, 'rb')

except:

raise ReadFailException, "unopen file[%s]\n" % filename

data = exifread.process_file( fd )

if data:

try:

t = data['EXIF DateTimeOriginal']

return str(t).replace(":",".")[:7]

except:

pass

state = os.stat(filename)

return time.strftime("%Y.%m", time.localtime(state[-2]))

def classifyPictures(path):

for root,dirs,files in os.walk(path,True):

dirs[:] = []

for filename in files:

filename = os.path.join(root, filename)

f,e = os.path.splitext(filename)

if e.lower() not in ('.jpg','.png','.mp4'):

continue

info = "文件名: " + filename + " "

t=""

try:

t = getOriginalDate( filename )

except Exception,e:

print e

continue

info = info + "拍摄时间:" + t + " "

pwd = root +'\\'+ t

dst = pwd + '\\' + filename

if not os.path.exists(pwd ):

os.mkdir(pwd)

print info, dst

shutil.copy2( filename, dst )

os.remove( filename )

if __name__ == "__main__":

path = "."

classifyPictures(path)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值