Python:如何在 add() 的过滤方法中访问 tarfile.add() 的 'name' 参数?

Python: how could I access tarfile.add()'s 'name' parameter in add()'s filter method?

我想在使用 tarfile (python 3.4 创建 tar(gz) 文件时过滤子目录(跳过它们) ).

磁盘上的文件:

已尝试将 /home/myuser/temp/test1/ 压缩为 tarfile.add()

我使用有路径和无路径模式。使用完整路径没问题,但是使用短路径我遇到了这个问题: 目录排除不起作用,因为 tarfile.add() 将 arcname 参数传递给过滤方法 - 而不是 name 参数!

archive.add(entry, arcname=os.path.basename(entry), filter=self.filter_general)

示例:

文件:/home/myuser/temp/test1/thing/bar.jpg -> arcname = test1/thing/bar.jpg

所以因为 exclude_dir_fullpath 中的 /home/myuser/temp/test1/thing 元素,过滤方法应该排除这个文件,但它不能因为过滤方法得到 test1/thing/bar.jpg.

如何访问 tarfile.add() 的 'name' 过滤器方法中的参数?

def filter_general(item):
    exclude_dir_fullpath = ['/home/myuser/temp/test1/thing', '/home/myuser/temp/test1/lemon']
    if any(dirname in item.name for dirname in exclude_dir_fullpath):
        print("Exclude fullpath dir matched at: %s" % item.name)  # DEBUG
        return None
    return item


def compress_tar():
    filepath = '/tmp/test.tar.gz'
    include_dir = '/home/myuser/temp/test1/'
    archive = tarfile.open(name=filepath, mode="w:gz")
    archive.add(include_dir, arcname=os.path.basename(include_dir), filter=filter_general)

compress_tar()

您想创建一个 general/re-useable 函数来过滤掉给定绝对路径名的文件。我知道仅对存档名称进行过滤是不够的,因为有时可以根据文件的来源来包含或不包含文件。

首先,为您的过滤器函数添加一个参数

def filter_general(item,root_dir):
    full_path = os.path.join(root_dir,item.name)

然后,将您的 "add to archive" 代码行替换为:

archive.add(include_dir, arcname=os.path.basename(include_dir), filter=lambda x: filter_general(x,os.path.dirname(include_dir)))

过滤器函数已被 lambda 替换,它传递包含目录的目录名称(否则,根目录将重复)

现在您的过滤函数知道根目录,您可以按绝对路径过滤,允许您在代码中的多个位置重复使用您的过滤函数。