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

    • 全栈之路
    • 😎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 标准库系列之 tarfile 模块

The tarfile module makes it possible to read and write tar archives, including those using gzip, bz2 and lzma compression. Use the zipfile module to read or write .zip files, or the higher-level functions in shutil.

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

打包及重命名文件

>>> import tarfile
# 以w模式创建文件
>>> tar = tarfile.open('tar_file.tar','w')
# 添加一个文件,arcname可以重命名文件
>>> tar.add('/tmp/folder/file.txt', arcname='file.log')
# 添加一个目录
>>> tar.add('/tmp/folder/tmp')                         
# 关闭
>>> tar.close()
1
2
3
4
5
6
7
8
9

查看文件列表

>>> tar = tarfile.open('tar_file.tar','r')             
# 获取包内的所有文件列表
>>> tar.getmembers()
[<TarInfo 'file.log' at 0x7f737af2da70>, <TarInfo 'tmp/folder/tmp' at 0x7f737af2dd90>]
1
2
3
4

追加

# 以w模式创建文件
>>> tar = tarfile.open('tar_file.tar','a')
>>> tar.add('/tmp/folder/sc.pyc')
>>> tar.close()
>>> tar = tarfile.open('tar_file.tar','r')
>>> tar.getmembers()
[<TarInfo 'file.log' at 0x7ff8d4fa1110>, <TarInfo 'tmp/folder/tmp' at 0x7ff8d4fa11d8>, <TarInfo 'tmp/folder/sc.pyc' at 0x7ff8d4fa12a0>]
1
2
3
4
5
6
7

解压全部文件

>>> import os
>>> import tarfile
>>> os.system("ls -l")
总用量 12
-rw-rw-r-- 1 ansheng ansheng 10240 5月  26 17:40 tar_file.tar
0
>>> tar = tarfile.open('tar_file.tar','r')
>>> tar.extractall()
>>> tar.close()
>>> os.system("ls -l")
总用量 16
-rw-rw-r-- 1 ansheng ansheng     0 5月  26 16:05 file.log
-rw-rw-r-- 1 ansheng ansheng 10240 5月  26 17:40 tar_file.tar
drwxrwxr-x 3 ansheng ansheng  4096 5月  26 17:48 tmp
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

解压单个文件

如果我们的压缩包很大的情况下,就不能够一次性解压了,那样太耗内存了,可以通过下面的方式进行解压,其原理就是一个文件一个文件的解压。

import tarfile
tar = tarfile.open('tar_file.tar','r')
for n in tar.getmembers():
    tar.extract(n,"/tmp")
tar.close()
1
2
3
4
5
编辑 (opens new window)
#编码#面向对象
上次更新: 2024-07-23, 01:00:43
Python 标准库系列之 sys 模块
Python 标准库系列之 time 模块

← Python 标准库系列之 sys 模块 Python 标准库系列之 time 模块→

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