鱼C论坛

 找回密码
 立即注册
查看: 2017|回复: 0

[作品展示] Python3 打包指定日期期间的文件

[复制链接]
发表于 2021-1-23 14:07:13 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Cool_Breeze 于 2021-1-23 14:34 编辑

# 被打包的文件日期,指的是文件最后修改日期
# 增量打包需要安装 rar (不会复制文件, 但是每次启动慢)
# shutil 打包需要复制文件到指定目录,然后在打包
  1. # coding=utf-8
  2. # python3.7.9

  3. import os
  4. import time
  5. import shutil
  6. import configparser
  7. from threading import Thread
  8. from queue import Queue
  9. from subprocess import run


  10. class zipConfig(object):
  11.     def __init__(self, configName):
  12.         try:
  13.             self.config = configparser.ConfigParser()
  14.             self.config.read(configName)
  15.             self.zipRoot = self.config['shutil']['root']                 # 打包父目录
  16.             self.zipBase = self.config['shutil']['base']                 # 指定打包的子目录
  17.             self.zipName = self.config['shutil']['name']                 # 打包文件名
  18.             self.zipF    = self.config['shutil']['format']               # 打包文件格式 zip
  19.             self.zipSD   = self.config['shutil']['sdate']                # 起始日期
  20.             self.zipED   = self.config['shutil']['edate']                # 结束日期
  21.             self.zipFD   = self.config['shutil']['dirname']              # 把符合要求的文件复制到这个目录
  22.             self.zipSrc  = self.config['shutil']['dir']                  # 待处理目录
  23.             self.zipRarPro   = self.config['rarexe']['rar']              # RaR程序的绝对路径名
  24.             self.zipRarName  = self.config['rarexe']['name']             # RaR打包后的文件名
  25.             self.zipshutil   = self.config.getboolean('rarexe','shutil') # 是否使用shutil打包文件
  26.             # 将日期转换为时间戳
  27.             self.sTimestamp = time.mktime(time.strptime(self.zipSD,'%Y-%m-%d'))
  28.             self.eTimestamp = time.mktime(time.strptime(self.zipED,'%Y-%m-%d'))
  29.             
  30.         except Exception as err:
  31.             print(err)
  32.             raise

  33. # shutil 复制一个文件
  34. def shutilCopyFile(queue):
  35.     global zipc
  36.     while True:
  37.         data = queue.get()
  38.         if data is None: break
  39.         print(f'正在复制文件 {data}')
  40.         shutil.copy(data, zipc.zipFD)
  41.     exit(0)

  42. # rar 增量打包文件
  43. def rar(queue, fileName):
  44.     global zipc
  45.     while True:
  46.         data = queue.get()
  47.         if data is None: break
  48.         cmd = f'{zipc.zipRarPro} -ep -y a {fileName} {data}'
  49.         if run(cmd).returncode == 0:
  50.             print(f'添加成功 {data}')
  51.         else: print(f'添加失败 {data}')
  52.     exit(0)

  53. # 过滤不满足要求的文件
  54. def filterFile(zipc):
  55.     queue = Queue()
  56.     if zipc.zipshutil:
  57.         if not os.path.exists(zipc.zipFD): os.mkdir(zipc.zipFD)
  58.         t = Thread(target=shutilCopyFile, args=(queue,))
  59.     else:
  60.         t = Thread(target=rar, args=(queue, zipc.zipRarName))
  61.     t.start()
  62.    
  63.     # 遍历文件目录
  64.     for dirPath, dirNames, fileNames in os.walk(zipc.zipSrc):
  65.         for file in fileNames:
  66.             src = os.path.join(dirPath,file)
  67.             # 获取文件最后修改时间 getmtime
  68.             # 获取文件最后访问时间 getatime
  69.             # 获取文件创建时间win  getctime
  70.             if zipc.sTimestamp < os.path.getmtime(src) < zipc.eTimestamp:
  71.                 queue.put(src)
  72.    
  73.     # 退出子线程
  74.     queue.put(None)
  75.     t.join()
  76.     if zipc.zipshutil:
  77.         print('正在打包文件')
  78.         # shutil 打包文件
  79.         print(shutil.make_archive(zipc.zipName, zipc.zipF, zipc.zipRoot, zipc.zipBase))
  80.         # 删除复制的文件
  81.         shutil.rmtree(zipc.zipFD)
  82.         

  83. if __name__ == '__main__':
  84.     zipc = zipConfig('zip.ini')
  85.     filterFile(zipc)
复制代码


配置文件 zip.ini :
  1. [DEFAULT]
  2. # 调用shutil打包
  3. shutil = true

  4. # 待处理目录
  5. dir = D:\GIN\py\sorting_method
  6. # 起始日期
  7. sdate = 2020-12-1
  8. # 结束日期
  9. edate = 2020-12-31


  10. [rarexe]
  11. # 文件名
  12. # name = d:\gin\cool
  13. name = cool.zip
  14. rar = C:\Program Files\WinRAR\winrar.exe



  15. [shutil]
  16. # 打包父目录
  17. root = ./

  18. # 打包文件格式 zip
  19. format = zip
  20. name = cool

  21. # 指定打包的子目录
  22. base = ./filterTemp

  23. # 把符合要求的文件复制到这个目录
  24. dirname = filterTemp
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
hrp + 3 + 3

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-28 18:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表