当前位置: 首页 > Python编程 > Python编程实战技能 > Python编程基础入门 > python如何生成tar文件内容

python如何生成tar文件内容

发布时间:2020年09月27日 10:03:21 来源: 点击量:621

【摘要】tarfile包中的 open(name, mode)方法能够以mode指定的方式打开name压缩文件,并返回一个TarFile类对象。调用TarFile对象的extractall(path

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)。

相关推荐:《Python教程》

如下是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

分享到: 编辑:wangmin

就业培训申请领取
您的姓名
您的电话
意向课程
点击领取

环球青藤

官方QQ

扫描上方二维码或点击一键加群,免费领取大礼包,加群暗号:青藤。 一键加群

绑定手机号

应《中华人民共和国网络安全法》加强实名认证机制要求,同时为更加全面的体验产品服务,烦请您绑定手机号.

预约成功

本直播为付费学员的直播课节

请您购买课程后再预约

环球青藤移动课堂APP 直播、听课。职达未来!

安卓版

下载

iPhone版

下载
环球青藤官方微信服务平台

刷题看课 APP下载

免费直播 一键购课

代报名等人工服务

课程咨询 学员服务 公众号

扫描关注微信公众号

APP

扫描下载APP

返回顶部