问题:如何使用Python创建完整的压缩tar文件?
How can I create a .tar.gz file with compression in Python?
回答 0
为整个目录树构建一个.tar.gz
(aka .tgz
):
import tarfile
import os.path
def make_tarfile(output_filename, source_dir):
with tarfile.open(output_filename, "w:gz") as tar:
tar.add(source_dir, arcname=os.path.basename(source_dir))
这将创建一个压缩的tar归档文件,其中包含一个名称和内容与相同的单个顶级文件夹source_dir
。
To build a .tar.gz
(aka .tgz
) for an entire directory tree:
import tarfile
import os.path
def make_tarfile(output_filename, source_dir):
with tarfile.open(output_filename, "w:gz") as tar:
tar.add(source_dir, arcname=os.path.basename(source_dir))
This will create a gzipped tar archive containing a single top-level folder with the same name and contents as source_dir
.
回答 1
import tarfile
tar = tarfile.open("sample.tar.gz", "w:gz")
for name in ["file1", "file2", "file3"]:
tar.add(name)
tar.close()
如果要创建tar.bz2压缩文件,只需将文件扩展名替换为“ .tar.bz2”,将“ w:gz”替换为“ w:bz2”。
import tarfile
tar = tarfile.open("sample.tar.gz", "w:gz")
for name in ["file1", "file2", "file3"]:
tar.add(name)
tar.close()
If you want to create a tar.bz2 compressed file, just replace file extension name with “.tar.bz2” and “w:gz” with “w:bz2”.
回答 2
你叫tarfile.open与mode='w:gz'
,意为“开放的gzip压缩的写作。”
您可能希望以结束文件名(的name
参数open
).tar.gz
,但这不会影响压缩功能。
顺便说一句,通常您可以使用的方式获得更好的压缩效果'w:bz2'
,就像tar
通常使用bzip2
时可以压缩甚至比使用时可以压缩得更好gzip
。
You call tarfile.open with mode='w:gz'
, meaning “Open for gzip compressed writing.”
You’ll probably want to end the filename (the name
argument to open
) with .tar.gz
, but that doesn’t affect compression abilities.
BTW, you usually get better compression with a mode of 'w:bz2'
, just like tar
can usually compress even better with bzip2
than it can compress with gzip
.
回答 3
先前的答案建议使用tarfile
Python模块.tar.gz
在Python中创建文件。这显然是一个不错的Python风格的解决方案,但是它在归档速度方面存在严重缺陷。这个问题提到的tarfile
速度大约是tar
Linux中实用程序。根据我的经验,这一估计是非常正确的。
因此,为了加快归档速度,可以使用tar
using subprocess
模块命令:
subprocess.call(['tar', '-czf', output_filename, file_to_archive])
Previous answers advise using the tarfile
Python module for creating a .tar.gz
file in Python. That’s obviously a good and Python-style solution, but it has serious drawback in speed of the archiving. This question mentions that tarfile
is approximately two times slower than the tar
utility in Linux. According to my experience this estimation is pretty correct.
So for faster archiving you can use the tar
command using subprocess
module:
subprocess.call(['tar', '-czf', output_filename, file_to_archive])
回答 4
在此tar.gz文件中,在打开的视图目录中压缩要解决,请使用os.path.basename(file_directory)
with tarfile.open("save.tar.gz","w:gz"):
for file in ["a.txt","b.log","c.png"]:
tar.add(os.path.basename(file))
它在tar.gz文件中的使用压缩在目录中
In this
tar.gz file compress in open view directory
In solve use os.path.basename(file_directory)
with tarfile.open("save.tar.gz","w:gz"):
for file in ["a.txt","b.log","c.png"]:
tar.add(os.path.basename(file))
its use in tar.gz file compress in directory
回答 5
除了@Aleksandr Tukallo的答案外,您还可以获得输出和错误消息(如果发生)。tar
在以下答案中很好地解释了使用压缩文件夹。
import traceback
import subprocess
try:
cmd = ['tar', 'czfj', output_filename, file_to_archive]
output = subprocess.check_output(cmd).decode("utf-8").strip()
print(output)
except Exception:
print(f"E: {traceback.format_exc()}")
In addition to @Aleksandr Tukallo’s answer, you could also obtain the output and error message (if occurs). Compressing a folder using tar
is explained pretty well on the following answer.
import traceback
import subprocess
try:
cmd = ['tar', 'czfj', output_filename, file_to_archive]
output = subprocess.check_output(cmd).decode("utf-8").strip()
print(output)
except Exception:
print(f"E: {traceback.format_exc()}")