面对堆积如山的照片,手工归类太烦易出错,这里用 python 写了一段程序,可以实现具有拍摄时间照片的自动归类,符合条件的文件被移走了,不符合的没有变化,使用者只需根据自己的需要修改 PhotoPath 和 NewPath 的值即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| import os import sys from shutil import Error from shutil import copystat from shutil import copy2 import exifread import shutil
PhotoPath = r'D:/photo' NewPath = r'D:/pushiji/ 图片 / 生命时间轴 P/'
def copy_file(src_file,dst_dir): if not os.path.isdir (dst_dir): os.makedirs (dst_dir) copy2 (src_file,dst_dir)
def move_file(src_file,dst_dir): if not os.path.isdir (dst_dir): os.makedirs (dst_dir) shutil.move (src_file,dst_dir)
def walk_file(file_path): for root,dirs,files in os.walk (file_path,topdown=False): for name in files: photo = os.path.join (root,name) try: with open (photo, 'rb') as img: dateStr = str (exifread.process_file (img)['Image DateTime']) year = dateStr [0:4] month = dateStr [5:7] new_path = NewPath+year+' 年 /'+year+' 年 '+month+' 月 /' move_file (photo,new_path) print ("moved '{}' to '{}'".format (photo,new_path)) except: print ("Movement failed. {}".format (photo)) for name in dirs: walk_file (name) walk_file (PhotoPath)
|