【问题标题】:What is the correct way to create "tgz" file in python? [duplicate]在 python 中创建“tgz”文件的正确方法是什么? [复制]
【发布时间】:2014-08-23 06:42:50
【问题描述】:

我使用下面的代码来创建 tgz 文件。当我尝试在压缩后提取 tgz 文件时,我在 Linux 上遇到了问题。但是当我尝试在 FreeBSD 上时,它会成功提取。

import tarfile
import os


def create_tgzfile(output_filename, source_dir,item_list):
    os.chdir(source_dir)
    tar = tarfile.open(output_filename+".tar.gz", "w")
    for name in item_list:
        tar.add(name)
    tar.close()


create_tgzfile("test","./",["file1","file2"])

test.tgz

在 FreeBSD 上提取

tar -xzvf test.tgz
x file1
x file2

在 Linux 上提取

tar -xzvf test.tgz
gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error is not recoverable: exiting now

那么,为什么它不能在 Linux 上打开或者如何使用 python 创建正确的 tgz 文件?

【问题讨论】:

  • 你创建了只是一个.tar文件,你没有压缩文件。

标签: python linux compression tar freebsd


【解决方案1】:

请尝试使用

tar = tarfile.open(output_filename+".tar.gz", "w:gz")

【讨论】:

    猜你喜欢
    • 2011-09-20
    • 1970-01-01
    • 2016-10-17
    • 1970-01-01
    • 2019-01-27
    • 2013-12-07
    • 1970-01-01
    • 1970-01-01
    • 2012-06-01
    相关资源
    最近更新 更多