当前位置:   article > 正文

Python 用 py7zr压缩文件_pyhton py7zr archive.write 把路径也压缩进去了

pyhton py7zr archive.write 把路径也压缩进去了

py7zr 是一个开源库,详细的说明请参阅Github:py7zr

代码简单示范压缩:(解压的方法类似,可参考github上的示例)

  1. 代码py文件的所在路径是:E:
  2. 要压缩的文件所在路径:E:\data
  3. 压缩后的7z文件路径是:E:
import os
import py7zr
import time

#获取py文件所在路径
dir = os.path.dirname(os.path.realpath('__file__')) 

#系统时间
now_date = time.strftime("%Y-%m-%d")
#压缩档名称
archive_name = now_date+"_MarketData.7z"  
archive = py7zr.SevenZipFile(archive_name, 'w')

#要压缩的文件夹路径
data_file = dir+"\\data"
if os.path.exists(data_file):
    for dir_path, dir_names, file_names in os.walk(data_file):
        for filename in file_names:
            print(filename)
            fpath = dir_path.replace(data_file, '')
            file_path = os.path.join(dir_path, filename)
            filename = os.path.join(fpath, filename)
            archive.write(file_path, arcname=filename)
    archive.close()
    print("压缩完成!")
else:
    print("文件不存在。")
  • 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

代码参考了 python使用py7zr做加密压缩

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/746300
推荐阅读
  

闽ICP备14008679号