tar = tarfile.open(save_directory, "w:gz")
for f in ["a.txt","b.png","c.doc"]:
tar.add(os.path.basename(f))
tar.close()
如何正确地将多个文件压缩为.tar.gz
格式?
答案 0 :(得分:0)
with
打开文件,一旦with
块退出,即使出现错误,也会自动关闭文件。import tarfile
file_list = ["a.txt","b.png","c.doc"]
with tarfile.open('packages.tar.gz', mode='w:gz') as tar:
for file in file_list:
tar.add(file)