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格式编码的压缩和解压缩的,由于是 ...
随机推荐
- 2019-11-8 Linux作业 李宗盛
linux系统的特点:完全免费,高效,安全稳定.支持多种硬件平台.有好的用户界面.强大的网络功能.支持多任务多用户. linux一般有三个主要部分:内核.命令解释层.实用工具. 那合适系统的心脏,是运 ...
- vue警告: component lists rendered with v-for should have explicit keys
warning信息:component lists rendered with v-for should have explicit keys. See https://vuejs.org/guide ...
- CenOS 7 安装Tomcat
1.首先需要安装jdk yum install java-1.8.0-openjdk-devel.x86_64 2.下载 http://mirrors.tuna.tsinghua.edu.cn/apa ...
- spring boot 单选按钮
jsp代码 性别: <form:radiobutton path="xb" value="男"/>男 <form:radiobutton pa ...
- Celery—分布式的异步任务处理系统
Celery 1.什么是Clelery Celery是一个简单.灵活且可靠的,处理大量消息的分布式系统 专注于实时处理的异步任务队列 同时也支持任务调度 Celery架构 Celery的架构由三部分组 ...
- python 脚本定时删除 elk索引
脚本如下 一.python 脚本如下 #! /usr/bin/python # -*- coding=utf-8 -*- import urllib import urllib.request imp ...
- Counting Cliques(HDU-5952)【DFS】
题目链接:https://vjudge.net/problem/HDU-5952 题意:有一张无向图,求结点数量为S的团的数量. 思路:如果不加一点处理直接用DFS必然会超时,因为在搜索过程中会出现遍 ...
- Python3 和 Python2的区别
目录 print Python2.7的print不是一个function Python3里的print是一个function. Unicode Python 2 有 ASCII str() 类型,un ...
- Board Game CodeForces - 605D (BFS)
大意: 给定$n$张卡$(a_i,b_i,c_i,d_i)$, 初始坐标$(0,0)$. 假设当前在$(x,y)$, 若$x\ge a_i,y\ge b_i$, 则可以使用第$i$张卡, 使用后达到坐 ...
- ABP的UnitOfWork内部SaveChanges无效
应用层一个AppService默认是一个工作单元,默认是开启的,默认是事务的.因为应用服务方法应该是原子的且一般都会使用数据库. 但是有些情况需要关闭工作单元 1.AppService有多个操作需要操 ...