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格式编码的压缩和解压缩的,由于是 ...
随机推荐
- Xena L23网络测试仪Valkyrie使用技巧100例,目录
Xena L23网络测试仪Valkyrie使用技巧100例,先写个目录 100例,会不会有点多,写不完... ^_^ 第1次编辑于2019-11-27 22:05:52, Evan YE. 编号 标题 ...
- NIO理解
ByteBuffer Test: package java_guide; import java.nio.ByteBuffer; public class ByteBufferMethods { pu ...
- mysql学习之join从句
一.join从句共有5种类型 内连接(inner join) 全外连接(full outer join) 左外连接(left outer join) 右外连接(right outer join) 交叉 ...
- 重新渲染layui的radio
修改后添加这一段 layui.use('form', function() { var form = layui.form; form.render(); }); 也可以用官方的方法:https:// ...
- Linux安装expect
主要参考:https://www.cnblogs.com/zhenbianshu/p/5867440.html expect解释器 expect是一个能实现自动和交互式任务的解释器,它也能解释常见的s ...
- wordpress 后台添加 快速编辑 栏目
前两篇其实是同一篇,都是讲在后台添加菜单类型的http://www.ashuwp.com/courses/highgrade/664.htmlhttps://shibashake.com/wordpr ...
- 使用网关zuul过滤器登录鉴权
使用网关zuul过滤器登录鉴权 1.新建一个filter包 filte有很多种 pre.post. 2.新建一个类LoginFilter,实现ZuulFilter,重写 ...
- go defer 语句会延迟函数的执行直到上层函数返回。
defer code... 可以理解为 执行完当前defer所在的方法代码后执行defer 中的代码 常用在释放资源 比如 关闭文件 为防止忘记编写关闭代码 可以先写好 defer 各种释放资源 ...
- 02 servlet基础 生命周期 tomcat web.xml
新建web项目 – new Web Project – 选择:javaee 5.0 建包 – com.gzsxt.wang 新建class:FirstServlet – 继承:HttpServlet( ...
- jacascript Math (算数)对象
前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! 实际应用中用的比较多的有:round(); random(); floor(); ceil(); 其次还有 ...