15Python标准库系列之tarfile模块

简介:

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


打包及重命名文件

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

追加

1
2
3
4
5
6
7
# 以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
8
9
10
11
12
13
14
15
>>>  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 - 3  ansheng ansheng   4096  5 月   26  17 : 48  tmp
0

解压单个文件

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

1
2
3
4
5
import  tarfile
tar  =  tarfile. open ( 'tar_file.tar' , 'r' )
for  in  tar.getmembers():
     tar.extract(n, "/tmp" )
tar.close()










本文转自 Edenwy  51CTO博客,原文链接:http://blog.51cto.com/edeny/1925793,如需转载请自行联系原作者
目录
相关文章
|
1天前
|
XML 前端开发 数据格式
BeautifulSoup 是一个 Python 库,用于从 HTML 和 XML 文件中提取数据
BeautifulSoup 是 Python 的一个库,用于解析 HTML 和 XML 文件,即使在格式不规范的情况下也能有效工作。通过创建 BeautifulSoup 对象并使用方法如 find_all 和 get,可以方便地提取和查找文档中的信息。以下是一段示例代码,展示如何安装库、解析 HTML 数据以及打印段落、链接和特定类名的元素。BeautifulSoup 还支持更复杂的查询和文档修改功能。
7 1
|
2天前
|
Python Windows
python中的异常与模块
python中的异常与模块
8 1
|
2天前
|
机器学习/深度学习 自然语言处理 算法
Gensim详细介绍和使用:一个Python文本建模库
Gensim详细介绍和使用:一个Python文本建模库
10 1
|
2天前
|
JSON 数据格式 Python
Python 的 requests 库是一个强大的 HTTP 客户端库,用于发送各种类型的 HTTP 请求
`requests` 库是 Python 中用于HTTP请求的强大工具。要开始使用,需通过 `pip install requests` 进行安装。发送GET请求可使用 `requests.get(url)`,而POST请求则需结合 `json.dumps(data)` 以JSON格式发送数据。PUT和DELETE请求类似,分别调用 `requests.put()` 和 `requests.delete()`。
13 2
|
3天前
|
JSON 数据格式 索引
python之JMESPath:JSON 查询语法库示例详解
python之JMESPath:JSON 查询语法库示例详解
14 0
|
10天前
|
Python
在Python中绘制K线图,可以使用matplotlib和mplfinance库
使用Python的matplotlib和mplfinance库可绘制金融K线图。mplfinance提供便利的绘图功能,示例代码显示如何加载CSV数据(含开盘、最高、最低、收盘价及成交量),并用`mpf.plot()`绘制K线图,设置类型为&#39;candle&#39;,显示移动平均线(mav)和成交量信息。可通过调整参数自定义图表样式,详情参考mplfinance文档。
30 2
|
11天前
|
机器学习/深度学习 边缘计算 TensorFlow
【Python机器学习专栏】Python机器学习工具与库的未来展望
【4月更文挑战第30天】本文探讨了Python在机器学习中的关键角色,重点介绍了Scikit-learn、TensorFlow和PyTorch等流行库。随着技术进步,未来Python机器学习工具将聚焦自动化、智能化、可解释性和可信赖性,并促进跨领域创新,结合云端与边缘计算,为各领域应用带来更高效、可靠的解决方案。
|
11天前
|
Serverless Python
使用Python的pandas和matplotlib库绘制移动平均线(MA)示例
使用Python的pandas和matplotlib库绘制移动平均线(MA)示例:加载CSV数据,计算5日、10日和20日MA,然后在K线图上绘制。通过`rolling()`计算平均值,`plot()`函数展示图表,`legend()`添加图例。可利用matplotlib参数自定义样式。查阅matplotlib文档以获取更多定制选项。
25 1
|
11天前
|
数据采集 SQL 数据挖掘
Python数据分析中的Pandas库应用指南
在数据科学和分析领域,Python语言已经成为了一种非常流行的工具。本文将介绍Python中的Pandas库,该库提供了强大的数据结构和数据分析工具,使得数据处理变得更加简单高效。通过详细的示例和应用指南,读者将了解到如何使用Pandas库进行数据加载、清洗、转换和分析,从而提升数据处理的效率和准确性。
|
11天前
|
SQL 关系型数据库 MySQL
使用Python的pymysql库连接MySQL,执行CRUD操作
使用Python的pymysql库连接MySQL,执行CRUD操作:安装pymysql,然后连接(host=&#39;localhost&#39;,user=&#39;root&#39;,password=&#39;yourpassword&#39;,database=&#39;yourdatabase&#39;),创建游标。查询数据示例:`SELECT * FROM yourtable`;插入数据:`INSERT INTO yourtable...`;更新数据:`UPDATE yourtable SET...`;删除数据:`DELETE FROM yourtable WHERE...`。
26 0