我正在try 使用在Docker容器中运行的应用程序中的Python来处理tar文件.因为我不经常使用tarfile,所以我起草了一些代码并在我本地的Python环境中运行它,这样在我在容器中运行它之前,我可以验证它是否以我想要的方式工作.
为简单起见,假设我有一个包含文件test_file.py
的目录a/
,并且我递归地将a/
压缩为tar文件file.tar.gz
.当我判断tar文件的内容时,我看到了目录和文本文件(a/
和a/test_file.py
).当我在本地环境中解压缩该目录时,我获得了该目录及其内容.当我在容器中运行相同的代码时,我只得到没有文件的空目录.遇到这个问题后,我做了一些搜索,找到了像Extract only a single directory from tar (in python)这样的帖子,它们明确建议包括您想要解压缩的所有文件,而不是假设它们会随父目录一起提供,所以我可以这样做.但是...
让我烦恼的是我的两个环境之间的这种不一致的行为!我的本地环境在Ubuntu 22.04上使用的是Python3.9.15.我的Docker容器在Ubuntu 18.04上使用的是Python3.10.9.因此,环境是不同的,但仍然存在这种差异似乎很奇怪.
以下是一些示例代码.我知道我之前提到过使用extract
的方法.我首先try 使用它,然后使用members
kwarg切换到extractall
,我得到了相同的行为--文件也在我的本地环境中存在,但不在Docker容器中.
import logging
import os
from pathlib import Path
import tarfile
import tempfile
def main():
with tempfile.TemporaryDirectory() as temp_dir:
# make a tar file with one directory that contains one file
file_path = Path(temp_dir) / "file.tar.gz"
os.chdir(temp_dir)
logging.info("Changed dir to %s", os.getcwd())
subdir = "a"
os.mkdir(subdir)
with open(Path(subdir) / "test_file.py", "w") as file_obj:
file_obj.write("import this\n")
with tarfile.open(file_path, "w:gz") as tar:
tar.add(subdir, recursive=True)
# unpack tar file
with tarfile.open(file_path) as tar:
file_names = tar.getnames()
logging.info(file_names)
members = [tar.getmember(subdir)]
tar.extractall(path=temp_dir, members=members)
# check contents of extracted directory
dir_path = Path(temp_dir) / subdir
output = os.listdir(dir_path)
logging.info("%s contents: %s", dir_path, output)
日志(log)记录显示,在两个环境中,目录和文件都在tar文件中(从call到tar.getnames()
),但最后listdir
调用的输出在一个环境中是文件,在另一个环境中是一个空列表.