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格式编码的压缩和解压缩的,由于是 ...
随机推荐
- 【计算机视觉】Selective Search for Object Recognition论文阅读2
Selective Search for Object Recognition 是J.R.R. Uijlings发表在2012 IJCV上的一篇文章.主要介绍了选择性搜索(Selective Sear ...
- Redis ZSet 有序集合
有序集合类型与集合类型的区别就是他是有序的.有序集合是在集合的基础上为每一个元素关联一个分数,这就让有序集合不仅支持插入,删除,判断元素是否存在等操作外,还支持获取分数最高/最低的前N个元素.有序集合 ...
- [转帖]Intel要提供2.5G的消费级以太网 价格2.4刀
千兆网已成过去!Intel将全面普及2.5Gbps以太网 https://news.cnblogs.com/n/641736/ 硬件发展突飞猛进 投递人 itwriter 发布于 2019-10-02 ...
- K8S从入门到放弃系列-(12)Kubernetes集群Coredns部署
摘要: 集群其他组件全部完成后我们应当部署集群 DNS 使 service 等能够正常解析,1.11版本coredns已经取代kube-dns成为集群默认dns. 1)下载yaml配置清单 [root ...
- Python 解LeetCode:744. Find Smallest Letter Greater Than Target
思路:二分法,时间复杂度o(logn) class Solution(object): def nextGreatestLetter(self, letters, target): "&qu ...
- Java 代码读取properties文件
jdk1.6 package read;import java.io.File;import java.io.FileInputStream;import java.io.IOException;im ...
- 开始使用 git(配置+常用命令)
▶ 注意 页面显示问题: -- 是两个短横线 - 是一个横短线 由于显示问题导致两个短横线之间没有空格,看起来像是一条横线,实则是两条短横线 ▶ git 常用命令 ◆ git add ● git ad ...
- uboot 添加自定义命令
ref : https://www.cnblogs.com/FREMONT/p/9824226.html 1.添加命令 1.1在common目录下,新建一个cmd_xx.c, 需要添加的命令格式为: ...
- hdu 4324
思路待整理 #include<cstdio> #include<iostream> #include<vector> #include<queue> # ...
- (十)SpringBoot之web 应用开发-Servlets, Filters, listeners
一.需求 Web 开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到 Servlet. FilterListene 二.案例 2.1 通过注册 ServletRegistr ...