按照时间整理分类python一键按照时间整理照片源代码

01

02

03

04

05

06

07

08

09

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

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

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:

            = 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:

        = getOriginalDate(file_path_name)

    except Exception as e:

        print(e)

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

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

    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)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大飞哥软件自习室

希望支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值