使用 Python 3.6 和 tarfile 模块在 Windows 10 上创建 .tar.gz 存档

Create .tar.gz archive on Windows 10 using Python 3.6 and tarfile module

我正在尝试在 Windows 10 文件夹中创建一个 tar.gz 存档文件。我的想法是,在我的文件夹中,我从我的输入文件开始,以我的输入文件和同名的 .tar.gz 存档结束。

问题是我的代码似乎对原始文件应用了某种压缩,而不是创建文件类型为 .tar.gz 的新文件。

代码如下。有人可以帮忙吗?

import os
import tarfile

full_path = 'C:\Myfolder\MyArchive.txt'
my_root = 'C:\MyFolder\'

tar = tarfile.open(full_path, "w:gz")
tar.add(my_root)
tar.close()

这将在与原始文件相同的目录中创建一个 .tar.gz 文件,其中只有一个文件后缀为 .tar.gz。也许这就是您要找的东西?

import tarfile

original_path = "C:\Myfolder\MyArchive.txt"
tgz_path = original_path + ".tar.gz"

with tarfile.open(tgz_path, "w:gz") as tar:
   tar.add(original_path)