Python中常用的压缩模块有zipfile、tarfile、gzip

1.zipfile模块的简单使用

import zipfile

# 压缩
z1 = zipfile.ZipFile('zip_test', 'w')
z1.write('src')
z1.write('dst4')
z1.close() # 解压缩
with zipfile.ZipFile('zip_test', 'r') as z2:
print(z2.read('src').decode()) # 查看压缩包中src文件内容
z2.extractall('zip123') # 解压文件到zip123目录下

2.tarfile模块的简单使用

import tarfile

# 压缩
t1 = tarfile.TarFile('tar_test', 'w')
t1.add('src', 'dst2')
t1.close() # 解压缩
with tarfile.TarFile('tar_test') as t2:
t2.extractall('tar123') # 解压文件到tar123目录下

3.gzip模块的简单使用

import gzip

f = open('src', encoding='utf-8').read()
f = f.encode(encoding='utf-8')
print(type(f)) with gzip.GzipFile('gzip_test', 'w') as g1:
g1.write(f) with gzip.GzipFile('gzip_test') as g2:
print(g2.read().decode())
print(type(g2))

Python压缩&解压缩的更多相关文章

  1. python 压缩 解压缩 文件

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

  2. linux 软连接创建 压缩解压缩 linux的dns服务相关

    linux软连接创建 注意用绝对路径,语法如下 ln -s 目标文件绝对路径 软连接名字绝对路径 ln -s /小护士.txt /tmp/hs.txt 修改linux的PS1变量,命令提示符变量 PS ...

  3. shutil 拷贝 / 移动 / 压缩 / 解压缩

    # shutil_demo.py 高级文件操作(拷贝 / 移动 / 压缩 / 解压缩) import shutil def shutil_demo(): # 拷贝文件 shutil.copy2('fi ...

  4. Linux下的压缩解压缩命令详解

    linux zip命令zip -r myfile.zip ./*将当前目录下的所有文件和文件夹全部压缩成myfile.zip文件,-r表示递归压缩子目录下所有文件. 2.unzipunzip -o - ...

  5. Qt之QuaZIP(zip压缩/解压缩)

    简述 QuaZIP是使用Qt/C++对ZLIB进行简单封装的用于压缩及解压缩ZIP的开源库.适用于多种平台,利用它可以很方便的将单个或多个文件打包为zip文件,且打包后的zip文件可以通过其它工具打开 ...

  6. hadoop的压缩解压缩,reduce端join,map端join

    hadoop的压缩解压缩 hadoop对于常见的几种压缩算法对于我们的mapreduce都是内置支持,不需要我们关心.经过map之后,数据会产生输出经过shuffle,这个时候的shuffle过程特别 ...

  7. Linux/centos/redhat下各种压缩解压缩方式详解

    1.zip命令 zip -r myfile.zip ./* 将当前目录下的所有文件和文件夹全部压缩成myfile.zip文件,-r表示递归压缩子目录下所有文件. 2.unzip unzip -o -d ...

  8. ICSharpCode.SharpZipLib实现压缩解压缩

    最近,在项目中经常需要处理压缩和解压缩文件的操作.经过查找,发现了ICSharpCode.SharpZipLib.dll ,这是一个完全由c#编写的Zip, GZip.Tar . BZip2 类库,可 ...

  9. 使用 apache ant 轻松实现文件压缩/解压缩(转)

    原文地址:http://blog.csdn.net/irvine007/article/details/6779492 maven配置ant包: <dependency> <grou ...

随机推荐

  1. WebGL着色器渲染小游戏实战

    项目起因 经过对 GLSL 的了解,以及 shadertoy 上各种项目的洗礼,现在开发简单交互图形应该不是一个怎么困难的问题了.下面开始来对一些已有业务逻辑的项目做GLSL渲染器替换开发. 起因是看 ...

  2. LP-DDR 和其他 DDR

    一篇技術文檔比較 LP-DDR 和其他 DDR. 就觀念來說,LP-DDR 就是 Low Power 的 DDR:但就架構來說,LP-DDR 和其他 DDR 是截然不同的東西. 他們分屬不同的 JDE ...

  3. ssl 原理和建立连接过程

    ssl ("Secure Sockets Layer")加密原理 和https的关系https = http + ssl ssl 位置: SSL握手 证书主要作用是在SSL握手中, ...

  4. Oracle 整库备份还原

    http://www.mamicode.com/info-detail-2481866.html sql语句 system用户登陆 查看表空间和存放位置 select t1.name,t2.name ...

  5. Maven打包web项目报错:webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update)

    问题描述 使用Maven打包项目的时候,出现错误: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing ...

  6. let that = this用法解析

    这种情况就是在一个代码片段里this有可能代表不同的对象,而编码者希望this代表最初的对象

  7. Vue.js教程 1.前端框架学习介绍

    Vue.js教程 1.前端框架学习介绍 什么是Vue.js 为什么要学习流行框架 什么是Vue.js Vue.js 是目前最火的一个前端框架,React是最流行的一个前端框架(React除了开发网站, ...

  8. SVN错误:Attempted to lock an already-locked dir svn: Working copy locked

    VN错误:Attempted to lock an already-locked dir update D:/workspace20/SC_200/metadata -r 3398 --force   ...

  9. 逐浪CMS+百度小程序开源包发布-对接海量资源助力推广

    接入百度小程序海量流量就在使用Zoomla!逐浪CMS开发栈 这里有最强的CMS内核依托于国家高新企业Zoomla!逐浪之上 这里有最海量的用户流量依托于百度小程序 这里有最开放的技术栈 这里有最卓越 ...

  10. ajax的post请求获取kfc官网数据

    # _*_ coding : utf-8 _*_# @Time : 2021/11/2 13:45# @Author : 秋泊酱 # 1页 # http://www.kfc.com.cn/kfccda ...