使用Python解压缩tar文档的方法

1、 tarfile包中的.open(name, mode)方法可以以mode指定的方式打开name压缩文件,并返回一个TarFile类对象。调用TarFile对象的extractall(path)方法可以将tar文档解压到path指定的位置。

import tarfile
tar = tarfile.open( '*.tar.gz', mode = "r:gz") #"r:gz"表示 open for reading with gzip compression
tar.extractall(path='temp')  ### 将tar.gz文件解压到temp文件夹下
tar.close()

open返回的对象不但可以用来读文档数据('r': reading),还可以写('w': writing),附加('a': appending)。

如下是mode取值所对应的含义:

'r' or 'r:*' open for reading with transparent compression
'r:'         open for reading exclusively uncompressed
'r:gz'       open for reading with gzip compression
'r:bz2'      open for reading with bzip2 compression
'r:xz'       open for reading with lzma compression
'a' or 'a:'  open for appending, creating the file if necessary
'w' or 'w:'  open for writing without compression
'w:gz'       open for writing with gzip compression
'w:bz2'      open for writing with bzip2 compression
'w:xz'       open for writing with lzma compression

'x' or 'x:'  create a tarfile exclusively without compression, raise
             an exception if the file is already created
'x:gz'       create a gzip compressed tarfile, raise an exception
             if the file is already created
'x:bz2'      create a bzip2 compressed tarfile, raise an exception
             if the file is already created
'x:xz'       create an lzma compressed tarfile, raise an exception
             if the file is already created

'r|*'        open a stream of tar blocks with transparent compression
'r|'         open an uncompressed stream of tar blocks for reading
'r|gz'       open a gzip compressed stream of tar blocks
'r|bz2'      open a bzip2 compressed stream of tar blocks
'r|xz'       open an lzma compressed stream of tar blocks
'w|'         open an uncompressed stream for writing
'w|gz'       open a gzip compressed stream for writing
'w|bz2'      open a bzip2 compressed stream for writing
'w|xz'       open an lzma compressed stream for writing

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python提供了多种方法和库来进行文件解压缩。其中一种方法使用zipfile库,它可以用于解压缩zip文件。通过引入zipfile库并使用其中的ZipFile类,我们可以轻松地解压缩zip文件。以下是一个使用zipfile库解压缩zip文件的示例代码: ``` import zipfile # 创建ZipFile对象,指定要解压缩的zip文件路径 with zipfile.ZipFile('example.zip', 'r') as zip_ref: # 解压所有文件到指定目录 zip_ref.extractall('unzip_folder') # 注意:此处的'example.zip'是待解压缩的zip文件名,'unzip_folder'是要解压缩到的目标文件夹路径 ``` 另外,如果需要解压缩其他类型的文件,比如tar、gz、rar等,可以使用相应的库来进行解压缩。例如,要解压缩.7z文件,可以使用py7zr库,如下所示: ``` import py7zr # 创建SevenZipFile对象,指定要解压缩的.7z文件路径 with py7zr.SevenZipFile('example.7z', 'r') as archive: # 解压缩所有文件到指定目录 archive.extractall(path='unzip_folder') # 注意:此处的'example.7z'是待解压缩的.7z文件名,'unzip_folder'是要解压缩到的目标文件夹路径 ``` 以上代码示例展示了使用zipfile库和py7zr库解压缩文件的过程。根据你的需求和待解压缩的文件类型,选择适当的库来进行文件解压缩操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python 文件解压缩](https://blog.csdn.net/weixin_45459224/article/details/127120860)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [如何使用Python对文件进行压缩与解压缩](https://blog.csdn.net/xff123456_/article/details/123900450)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值