别院牧志知识库 别院牧志知识库
首页
  • 基础

    • 全栈之路
    • 😎Awesome资源
  • 进阶

    • Python 工匠系列
    • 高阶知识点
  • 指南教程

    • Socket 编程
    • 异步编程
    • PEP 系列
  • 面试

    • Python 面试题
    • 2025 面试记录
    • 2022 面试记录
    • 2021 面试记录
    • 2020 面试记录
    • 2019 面试记录
    • 数据库索引原理
  • 基金

    • 基金知识
    • 基金经理
  • 细读经典

    • 德隆-三个知道
    • 孔曼子-摊大饼理论
    • 配置者说-躺赢之路
    • 资水-建立自己的投资体系
    • 反脆弱
  • Git 参考手册
  • 提问的智慧
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
首页
  • 基础

    • 全栈之路
    • 😎Awesome资源
  • 进阶

    • Python 工匠系列
    • 高阶知识点
  • 指南教程

    • Socket 编程
    • 异步编程
    • PEP 系列
  • 面试

    • Python 面试题
    • 2025 面试记录
    • 2022 面试记录
    • 2021 面试记录
    • 2020 面试记录
    • 2019 面试记录
    • 数据库索引原理
  • 基金

    • 基金知识
    • 基金经理
  • 细读经典

    • 德隆-三个知道
    • 孔曼子-摊大饼理论
    • 配置者说-躺赢之路
    • 资水-建立自己的投资体系
    • 反脆弱
  • Git 参考手册
  • 提问的智慧
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 辨析

  • Sockets编程

  • Django

  • stackoverflow

  • Flask

  • 全栈之路

    • 基础篇

    • 进阶篇

    • 面向对象

    • 网络编程

    • 操作系统

    • 标准库

      • Python 标准库系列之 collections 模块
      • Python 标准库系列之 configparser 模块
      • Python 标准库系列之 datetime 模块
      • Python 标准库系列之 hashlib 模块
      • Python 标准库系列之模块介绍
      • Python 标准库系列之 json 模块
      • Python 标准库系列之 logging 模块
      • Python 标准库系列之 os 模块
      • Python 标准库系列之 random 模块
      • Python 标准库系列之 shutil 模块
      • Python 标准库系列之 subprocess 模块
      • Python 标准库系列之 sys 模块
      • Python 标准库系列之 tarfile 模块
      • Python 标准库系列之 time 模块
      • Python 标准库系列之 xml 模块
      • Python 标准库系列之 zipfile 模块
    • 第三方库

    • Python 全栈之路
  • 面试

  • 代码片段

  • 异步编程

  • 😎Awesome资源

  • PEP

  • Python工匠系列

  • 高阶知识点

  • Python 学习资源待整理
  • 设计模式

  • 好“艹蛋”的 Python 呀!
  • FIFO | 待学清单📝
  • pip 安装及使用
  • 数据分析

  • 源码阅读计划

  • OOP

  • 关于 python 中的 setup.py
  • 并行分布式框架 Celery
  • 七种武器,让你的代码提高可维护性
  • 使用 pdb 调试 Python 代码
  • 每周一个 Python 标准库
  • 🐍Python
  • 全栈之路
  • 标准库
佚名
2020-05-23

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

打包

>>> 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
9
10
11
12
13
14
15
16
17

追加一个文件

# 追加其实就是把模式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
4
5
6
7
8

查看压缩包内的所有文件

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

解压

>>> 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
1
2
3
4
5
6
7
8
9
10
11
编辑 (opens new window)
#编码#面向对象
上次更新: 2024-07-23, 01:00:43
Python 标准库系列之 xml 模块
Python 标准库系列之 Memcache 模块

← Python 标准库系列之 xml 模块 Python 标准库系列之 Memcache 模块→

最近更新
01
2025 面试记录
05-28
02
提升沟通亲和力的实用策略
03-26
03
工作
07-15
更多文章>
Theme by Vdoing | Copyright © 2019-2025 IMOYAO | 别院牧志
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式