【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. [Reversal 剧情设计] 序言

    想开发一个图形游戏已经很久了,这个寒假好不容易学了一些OpenGL知识,初步具备了开发图形游戏的能力,我觉得我可以开始了.当然,我的游戏并不是冷冰冰的PVE,肯定还需要一些剧情丰富色彩. 本来我的游戏 ...

  2. 吴裕雄 python 机器学习——数据预处理过滤式特征选取VarianceThreshold模型

    from sklearn.feature_selection import VarianceThreshold #数据预处理过滤式特征选取VarianceThreshold模型 def test_Va ...

  3. C#中发ref和out

    ref--Reference  引用 out--Output   输出 相同点: 代入参数时,前面必须加上ref  out 关键字 都能在方法内对外部的变量的值进行更改 不同点: ref代入的参数必须 ...

  4. jquery 获取 父级 iframe 里的控件对象

    window.parent.document.getElementsByTagName('iframe')[0].contentWindow.document.getElementById('id')

  5. cube-ui IndexList 切换Tab Y坐标归零

    <template> <div class="fx t12 column"> <div class="order_search_div fl ...

  6. 各种颜色空间之间的转换算法(XYZ → Standard-RGB ,Standard-RGB → XYZ)

    http://www.easyrgb.com/en/convert.php#Result http://www.easyrgb.com/en/math.php

  7. nginx反向代理配置及常见指令

    nginx配置文件: /usr/local/nginx1.16.1/conf/nginx.conf nginx.conf默认的server配置: server{ listen 80; server_n ...

  8. Django框架之ORM的相关操作(一)

    一.一般操作 from django.db import models import datetime # Create your models here. class Author(models.M ...

  9. opencv python:轮廓发现

    example import cv2 as cv import numpy as np def edge_demo(image): blurred = cv.GaussianBlur(image, ( ...

  10. 在 Fabric 中使用私有数据

    本教程将演示收集器(collection)的使用,收集器为区块链网络上已授权的组织节点 提供私有数据的存储和检索. 本教程假设您已了解私有数据的存储和他们的用例.更多的信息请参阅 私有数据 . 本教程 ...