python生成tar文件内容的方法

python生成tar文件内容的方法?这个问题可能是我们日常学习或工作经常见到的。希望通过这个问题能让你收获颇深。下面是小编给大家带来的参考内容,让我们一起来看看吧!

创新互联公司从2013年创立,先为东风等服务建站,东风等地企业,进行企业商务咨询服务。为东风企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

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

感谢各位的阅读!看完上述内容,你们对python生成tar文件内容的方法大概了解了吗?希望文章内容对大家有所帮助。如果想了解更多相关文章内容,欢迎关注创新互联行业资讯频道。

文章题目:python生成tar文件内容的方法
文章转载:https://www.cdcxhl.com/article14/pihsde.html

成都网站建设公司_创新互联,为您提供外贸网站建设响应式网站全网营销推广外贸建站手机网站建设网站维护

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

绵阳服务器托管