14Python标准库系列之zipfile模块

简介:

Python标准库系列之zipfile模块


The ZIP file format is a common archive and compression standard. This module provides tools to create, read, write, append, and list a ZIP file.

This module does not currently handle multi-disk ZIP files. It can handle ZIP files that use the ZIP64 extensions (that is ZIP files that are more than 4 GiB in size). It supports decryption of encrypted files in ZIP archives, but it currently cannot create an encrypted file. Decryption is extremely slow as it is implemented in native Python rather than C.

官方文档:https://docs.python.org/3.5/library/zipfile.html


打包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
>>>  import  zipfile
>>>  import  os
>>> os.system( "ls -l" )
总用量  0
0
# 以w的方式的时候是打开文件并清空,如果是a方式那么就是追加文件了
>>> z  =  zipfile.ZipFile( 'zip_file.zip' ,  'w' )
# 把文件放入压缩包
>>> z.write( '/tmp/folder/file.txt' )
# 也可以是一个目录
>>> z.write( '/tmp/folder/dir' )         
# 关闭文件
>>> z.close()
# 查看已经打包的文件
>>> os.system( "ls -l zip_file.zip" )                     
- rw - rw - r - -  1  ansheng ansheng  238  5 月   26  17 : 08  zip_file. zip
0

追加一个文件

1
2
3
4
5
6
7
8
# 追加其实就是把模式w换成a
>>> z  =  zipfile.ZipFile( 'zip_file.zip' ,  'a' )
>>> z.write( '/tmp/folder/file.txt' )         
# 关闭文件
>>> z.close()
# 查看包内的文件
>>> z.namelist()
[ 'tmp/folder/sc.pyc' ,  'tmp/folder/dir/' ,  'tmp/folder/file.txt' ]

查看压缩包内的所有文件

1
2
3
# z.namelist()获取压缩包内的所有文件,以列表形式返回
>>> z.namelist()
[ 'tmp/folder/sc.pyc' ,  'tmp/folder/dir/' ,  'tmp/folder/file.txt' ]

解压

1
2
3
4
5
6
7
8
9
10
11
>>> z  =  zipfile.ZipFile( 'zip_file.zip' ,  'r' )
# extractall把所有的文件解压到当前目录
>>> z.extractall()
>>> os.system( "tree tmp/" )         
tmp /
└── folder
     ├──  dir
     └── sc.pyc
 
2  directories,  1  file
0

解压一个单独的文件

1
2
3
4
5
6
7
8
9
10
11
>>> z  =  zipfile.ZipFile( 'zip_file.zip' ,  'r' )
# 返回文件所在路径
>>> z.extract( "tmp/folder/sc.pyc" )          
'/home/ansheng/tmp/folder/sc.pyc'
>>> os.system( "tree tmp/" )                  
tmp /
└── folder
     └── sc.pyc
     
1  directory,  1  file
0









本文转自 Edenwy  51CTO博客,原文链接:http://blog.51cto.com/edeny/1925763,如需转载请自行联系原作者
目录
相关文章
|
SQL 关系型数据库 数据库
Python SQLAlchemy模块:从入门到实战的数据库操作指南
免费提供Python+PyCharm编程环境,结合SQLAlchemy ORM框架详解数据库开发。涵盖连接配置、模型定义、CRUD操作、事务控制及Alembic迁移工具,以电商订单系统为例,深入讲解高并发场景下的性能优化与最佳实践,助你高效构建数据驱动应用。
1483 7
|
监控 安全 程序员
Python日志模块配置:从print到logging的优雅升级指南
从 `print` 到 `logging` 是 Python 开发的必经之路。`print` 调试简单却难维护,日志混乱、无法分级、缺乏上下文;而 `logging` 支持级别控制、多输出、结构化记录,助力项目可维护性升级。本文详解痛点、优势、迁移方案与最佳实践,助你构建专业日志系统,让程序“有记忆”。
1216 0
|
JSON 算法 API
Python中的json模块:从基础到进阶的实用指南
本文深入解析Python内置json模块的使用,涵盖序列化与反序列化核心函数、参数配置、中文处理、自定义对象转换及异常处理,并介绍性能优化与第三方库扩展,助你高效实现JSON数据交互。(238字)
1013 4
|
Java 调度 数据库
Python threading模块:多线程编程的实战指南
本文深入讲解Python多线程编程,涵盖threading模块的核心用法:线程创建、生命周期、同步机制(锁、信号量、条件变量)、线程通信(队列)、守护线程与线程池应用。结合实战案例,如多线程下载器,帮助开发者提升程序并发性能,适用于I/O密集型任务处理。
1156 0
|
XML JSON 数据处理
超越JSON:Python结构化数据处理模块全解析
本文深入解析Python中12个核心数据处理模块,涵盖csv、pandas、pickle、shelve、struct、configparser、xml、numpy、array、sqlite3和msgpack,覆盖表格处理、序列化、配置管理、科学计算等六大场景,结合真实案例与决策树,助你高效应对各类数据挑战。(238字)
1588 0
|
Python 人工智能 数据可视化
Python模块与包(八)
Python模块与包(八)
382 0
Python模块与包(八)
|
开发者 Python
如何在Python中管理模块和包的依赖关系?
在实际开发中,通常会结合多种方法来管理模块和包的依赖关系,以确保项目的顺利进行和可维护性。同时,要及时更新和解决依赖冲突等问题,以保证代码的稳定性和可靠性
851 159
|
Python
Python的模块和包
总之,模块和包是 Python 编程中非常重要的概念,掌握它们可以帮助我们更好地组织和管理代码,提高开发效率和代码质量
628 159
|
数据可视化 Python
如何在Python中解决模块和包的依赖冲突?
解决模块和包的依赖冲突需要综合运用多种方法,并且需要团队成员的共同努力和协作。通过合理的管理和解决冲突,可以提高项目的稳定性和可扩展性

推荐镜像

更多