【Python压缩文件夹】导入“zipfile”模块

 def zip_ya(startdir,file_news):
startdir = ".\\123" #要压缩的文件夹路径
file_news = startdir +'.zip' # 压缩后文件夹的名字
z = zipfile.ZipFile(file_news,'w',zipfile.ZIP_DEFLATED) #参数一:文件夹名
for dirpath, dirnames, filenames in os.walk(startdir):
fpath = dirpath.replace(startdir,'') #这一句很重要,不replace的话,就从根目录开始复制
fpath = fpath and fpath + os.sep or ''#这句话理解我也点郁闷,实现当前文件夹以及包含的所有文件的压缩
for filename in filenames:
z.write(os.path.join(dirpath, filename),fpath+filename)
print ('压缩成功')
z.close() if__name__=="__main__"
startdir = ".\\123" #要压缩的文件夹路径
file_news = startdir +'.zip' # 压缩后文件夹的名字
zip_ya(startdir,file_news)

【python压缩文件】导入“zipfile”模块

 import zipfile
def zip_files( files, zip_name ):
zip = zipfile.ZipFile( zip_name, 'w', zipfile.ZIP_DEFLATED )
for file in files:
print ('compressing', file)
zip.write( file )
zip.close()
print ('compressing finished') files = ['.\\123.txt','.\\3.txt']#文件的位置,多个文件用“,”隔开
zip_file = '.\\m66y.zip'#压缩包名字
zip_files(files, zip_file)

Python压缩文件/文件夹的更多相关文章

  1. Python压缩指定文件及文件夹为zip

    Python压缩指定的文件及文件夹为.zip 代码: def zipDir(dirpath,outFullName): """ 压缩指定文件夹 :param dirpat ...

  2. python 压缩 解压缩 文件

    1. 用zipfile模块打包文件或是目录.解压zip文件 http://wangwei007.blog.51cto.com/68019/1045577 #!/usr/bin/env python # ...

  3. 【Python】使用Python压缩文件/文件夹

    [Python压缩文件夹]导入“zipfile”模块 def zip_ya(startdir,file_news): startdir = ".\\123" #要压缩的文件夹路径 ...

  4. python实现压缩当前文件夹下的所有文件

    import os import zipfile def zipDir(dirpath, outFullName): ''' 压缩指定文件夹 :param dirpath: 目标文件夹路径 :para ...

  5. Python压缩文件夹 tar.gz .zip

    打包压缩生成 XXX.tar.gz 文件 import os import tarfile if os.path.exists(outputFileName): with tarfile.open(o ...

  6. SharpZipLib 文件/文件夹压缩

    一.ZipFile ZipFile类用于选择文件或文件夹进行压缩生成压缩包. 常用属性: 属性 说明 Count 文件数目(注意是在ComitUpdat之后才有) Password 压缩包密码 Siz ...

  7. albert1017 Linux下压缩某个文件夹(文件夹打包)

    albert1017 Linux下压缩某个文件夹(文件夹打包) tar -zcvf /home/xahot.tar.gz /xahottar -zcvf 打包后生成的文件名全路径 要打包的目录例子:把 ...

  8. linux压缩文件(夹) zip uzip命令的用法

    压缩文件(夹) # 压缩列举的文件,格式如下: zip 压缩包名称 文件1 文件2 文件3 ... # 压缩test.txt, a.out文件,并取名为abc.zip $ zip abc.zip te ...

  9. C#压缩、解压缩文件(夹)(rar、zip)

    主要是使用Rar.exe压缩解压文件(夹)(*.rar),另外还有使用SevenZipSharp.dll.zLib1.dll.7z.dll压缩解压文件(夹)(*.zip).需要注意的几点如下: 1.注 ...

随机推荐

  1. vue 实现上一周、下一周切换功能

    效果图: html 显示部分: js 显示部分: preNextBtn(val){ let _this = this; this.tableList = []; //数据重置为空 _this.show ...

  2. node 连接数据库异常

    1 找不到mysql模块 报错:Cannot find module 'mysql' 处理:npm install mysql 2 建立了多次连接 报错:Cannot enqueue Handshak ...

  3. 交换机的MAC地址?

    该示例中记录了Cat2960的MAC地址情况: Cat2960#sho mac address-table Mac Address Table----------------------------- ...

  4. Codeforces Global Round 6D(VECTOR<ARRAY<INT,3> >)

    一个人只要存在债务关系,那么他的债务可以和这整个债务关系网中任何人连边,和他当初借出或欠下的人没有关系.只需要记录他的债务值即可. #define HAVE_STRUCT_TIMESPEC #incl ...

  5. Angular 调用百度地图API接口

    Angular 调用百度地图API接口 参考原文:https://blog.csdn.net/yuyinghua0302/article/details/80624274 下面简单介绍一下如何在Ang ...

  6. 每天进步一点点------CORDIC (一)

    三角函数计算,Cordic 算法入门 三角函数的计算是个复杂的主题,有计算机之前,人们通常通过查找三角函数表来计算任意角度的三角函数的值.这种表格在人们刚刚产生三角函数的概念的时候就已经有了,它们通常 ...

  7. SniperOJ-as fast as you can-Writeup

    SniperOJ-as fast as you can-Writeup 题目描述: 打开所给的题目地址 很容易可以看出这是一个简单的爬虫问题,爬去网页的信息并post,但在界面及网页源码中均没发现有价 ...

  8. Spring整合JDBC和Druid连接池

    我的博客名为黑客之谜,喜欢我的,或者喜欢未来的大神,点一波关注吧!顺便说一下,双十二快到了,祝大家双十二快乐,尽情的买买买~ 如果转载我的文章请标明出处和著名,谢谢配合. 我的博客地址为: https ...

  9. 红帽RHCE培训-课程3笔记内容1

    1 控制服务和守护进程 systemctl systemctl start ** systemctl restart ** systemctl enable ** systemctl status * ...

  10. opencv python:图像二值化

    import cv2 as cv import numpy as np import matplotlib.pyplot as plt # 二值图像就是将灰度图转化成黑白图,没有灰,在一个值之前为黑, ...