python zipfile使用
the Python challenge中第6关使用到zipfile模块,于是记录下zipfile的使用
zip日常使用只要是压缩跟解压操作,于是从这里入手
1、压缩
f=zipfile.ZipFile(file, mode="r", compression=ZIP_STORED, allowZip64=False)
创建一个zip文件对象,压缩是需要把mode改为‘w’,这个是源码中的注释Open the ZIP file with mode read "r", write "w" or append "a",a为追加压缩,不会清空原来的zip
f.write(filename)
将文件写入zip文件中,即将文件压缩
f.close()
将zip文件对象关闭,与open一样可以使用上下文with as
import zipfile with zipfile.ZipFile('test.zip', mode='w') as zipf:
zipf.write('channel.zip')
zipf.write('zip_test.py') zipf = zipfile.ZipFile('test.zip')
print zipf.namelist()
2、解压
f.extract(directory)和f.exractall(directory)
import zipfile zipf = zipfile.ZipFile('test.zip') zipf.extractall('channel1')#将所有文件解压到channel1目录下
高级应用
1 zipfile.is_zipfile(filename)
判断一个文件是不是压缩文件
2 ZipFile.namelist()
返回文件列表
3 ZipFile.open(name[, mode[, password]])
打开压缩文档中的某个文件
if zipfile.is_zipfile('test.zip'): #is_zipfile() 判断是否似zip文件
f = zipfile.ZipFile('test.zip')
files = f.namelist() #namelist() 返回zip压缩包中的所有文件
print 'files:', files
mess = f.open('channel/readme.txt') #打开zip压缩包中的某个文件,可读可写
print 'mess:', mess.read()
f.close()
python zipfile使用的更多相关文章
- Python ZipFile模块详解(转)
Python zipfile模块用来做zip格式编码的压缩和解压缩的,zipfile里有两个非常重要的class, 分别是ZipFile和ZipInfo, 在绝大多数的情况下,我们只需要使用这两个cl ...
- python - zipfile
参考:http://www.cnblogs.com/sislcb/archive/2008/11/28/1342822.html zipfile - python处理zip文件的压缩与解压 ZipFi ...
- Python - zipfile 乱码问题解决
最近使用zipfile进行解包过程中遇到了很不舒服的问题,解包之后文件名是乱码的.下面进行简单总结: 首先,乱码肯定是因为解码方式不一样了,zipfile使用的是utf-8和cp437这两种编码方式, ...
- python zipfile 文件压缩和文件
文件压缩 zipfile_obj = zipfile.ZipFile(zipfile_objpath, 'a', zipfile.ZIP_DEFLATED) for dirpath, dirnames ...
- python - zipfile模块
import zipfile # f=zipfile.ZipFile(filename, mode="r", compression=ZIP_STORED, allowZip64= ...
- Python zipfile 编码问题
zipfile默认对于文件名编码只识别cp437和utf-8 对于采用其他编码方式的文件,zipfile解压出来的就会是乱码 我们可以先把它encode成cp437然后再decode成GBK 最后在把 ...
- Python zipfile模块学习
转载自https://www.j4ml.com/t/15270 import zipfile import os from zipfile import ZipFile class ZipManage ...
- python笔记之ZipFile模块
python笔记之ZipFile模块 zipfile模块用来做zip格式编码的压缩和解压缩的,zipfile里有两个非常重要的class, 分别是ZipFile和ZipInfo, 在绝大多数的情况下, ...
- python中zipfile模块实例化解析
文章内容由--“脚本之家“--提供,在此感谢脚本之家的贡献,该网站网址为:https://www.jb51.net/ 简介: zipfile是python里用来做zip格式编码的压缩和解压缩的,由于是 ...
随机推荐
- 如何配置docker仓库
创建文件 /etc/docker/daemon.json,写入国内镜像URL地址 { "registry-mirrors": [ "https://rq5uyt7.mir ...
- 神经网络与机器学习第3版学习笔记-第1章 Rosenblatt感知器
神经网络与机器学习第3版学习笔记 -初学者的笔记,记录花时间思考的各种疑惑 本文主要阐述该书在数学推导上一笔带过的地方.参考学习,在流畅理解书本内容的同时,还能温顾学过的数学知识,达到事半功倍的效果. ...
- mysql 查询结果为null 或 空字符串时,返回指定字符串
直接上代码, 亲测可用: SELECT IF ( ifnull( 字段, '' ) = '', '返回的字符串', 字段) AS 别名(或者不要也可以) FROM table
- cv2---imread---error
when I use the cv2.imred() which is absolute path path = r'C:\\Users\\hp\\Desktop\\常用Python代码\\mycv ...
- with as用法 --Python
有些任务,可能事先设置,时候做清理工作,如下面一段程序: f = open('tmp.txt') data = f.read() print(data) 是不是忘了什么?没错,很明显忘记关闭文件句柄. ...
- Java面试题代码篇
1.统计字符串中的各种字符的个数并对其排序 package JavaMianSiTest; public class TongJIZiFu { public static void main(Stri ...
- MySQL提供的几种检索行数据的优化方式
ICP(Index Condition Pushdown): 在MySQL5.6之前,存储引擎会通过遍历索引定位基表中 的行,然后返回给Server层,再去为这些数据进行WHERE后的条件过滤.MyS ...
- Redis慢日志取出来
http://blog.chinaunix.net/uid-31396856-id-5758295.htmlhttps://blog.51cto.com/legehappy/2151986?sourc ...
- 使用Duilib开发Windows软件(1)——HelloWorld
我使用的是网易版本: https://github.com/netease-im/NIM_Duilib_Framework 时间是2019-11-28,作者最新的提交如下图: 运行官方示例程序 下载完 ...
- Asp.net core 学习笔记 ( ef core transaction scope & change level )
ef core 有 unit of work 的概念,当我们 save change 时会自动使用 transaction 确保更新的一致性. 隔离级别是默认的 read committed 不允许脏 ...