[Python] 纯文本查看 复制代码 import shutil
import os
import time
import exifread
def getfiles(rootdir):
fileset = []
for home, dirs, files in os.walk(rootdir):
for filename in files:
print(filename)
fileset.append(os.path.join(home,filename))
return fileset
def getOriginalDate(filename):
try:
fd = open(filename, 'rb')
except Exception as e:
print(e)
data = exifread.process_file( fd )
if data:
try:
t = data['EXIF DateTimeOriginal']
return str(t).replace(":",".")[:7]
except Exception as e:
print(e)
state = os.stat(filename)
return time.strftime("%Y.%m", time.localtime(state[-2]))
def classifyPictures(file_path_name,des_path):
file_name = os.path.basename(file_path_name)
t=""
try:
t = getOriginalDate(file_path_name)
except Exception as e:
print(e)
info = "文件名: " + file_name + " "
info = info + "拍摄时间:" + t + " "
print(info)
dst = os.path.join(des_path,t)
if not os.path.exists(dst):
os.mkdir(dst)
shutil.copy2(file_path_name,dst)
if __name__ == "__main__":
source_path = 'D:\迅雷下载\图片视频' #待整理的图片目录
des_path = 'E:\\Picture' #整理后存放目录
fileset= getfiles(source_path) #获取待整理图片目录下所有文件地址
total = len(fileset) #待整理文件数量
for index,file_path_name in enumerate(fileset):
print('共{}个文件,正在处理{}个文件'.format(total,index))
classifyPictures(file_path_name,des_path)
|