知行编程网知行编程网  2022-09-20 03:30 知行编程网 隐藏边栏  19 
文章评分 0 次,平均分 0.0
导语: 本文主要介绍了关于python如何生成tar文件内容的相关知识,包括python docx,以及python复制文件到另一个目录这些编程知识,希望对大家有参考作用。

python如何生成tar文件内容

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':读取),还可以用于写入('w':写入)和追加('a':追加)。

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

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写
扫一扫二维码分享