Python tarfile并排除 [英] Python tarfile and excludes

查看:204
本文介绍了Python tarfile并排除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是Python文档的摘录:

This is an excerpt from Python's documentation:

如果给出了exclude,则它必须是一个使用一个文件名的函数 参数并返回一个布尔值.根据该值, 相应文件被排除(为真)或添加为(假).

If exclude is given it must be a function that takes one filename argument and returns a boolean value. Depending on this value the respective file is either excluded (True) or added (False).

我必须承认我不知道那是什么意思.

I must admit that I have no idea what that means.

此外:

从2.7版开始不推荐使用:exclude参数不推荐使用, 请改用filter参数.为了获得最大的便携性, 过滤器应用作关键字参数,而不是用作 位置参数,以便在排除为 最终被删除.

Deprecated since version 2.7: The exclude parameter is deprecated, please use the filter parameter instead. For maximum portability, filter should be used as a keyword argument rather than as a positional argument so that code won’t be affected when exclude is ultimately removed.

好...以及过滤器"的定义:

Ok... and the definition for "filter":

如果指定了过滤器,则它必须是采用TarInfo的函数 object参数,并返回更改后的TarInfo对象.如果相反 返回None,则将从存档中排除TarInfo对象.

If filter is specified it must be a function that takes a TarInfo object argument and returns the changed TarInfo object. If it instead returns None the TarInfo object will be excluded from the archive.

...回到第一格:)

... back to square one :)

我真正需要的是一种将排除项的数组(或:"定界字符串)传递到tarfile.add的方法.

What I really need is a way to pass an array (or a ":" delimited string) of excludes to the tarfile.add.

如果您尝试解释PyDocs的这些段落,我不会介意.

I would not mind if you try to explain what those passages from PyDocs ment.

P.S.:

这只是我的主意:

  • 制作一个包含源目录内容列表的数组
  • 弹出排除
  • 对剩下的单个数组成员执行tar.add

但是,我希望它以一种更有文化底蕴的方式完成

But, I'd like it done in a more cultured way

推荐答案

如果给出了exclude,则它必须是一个使用一个文件名的函数 参数并返回一个布尔值.根据该值, 相应文件被排除(为真)或添加为(假).

If exclude is given it must be a function that takes one filename argument and returns a boolean value. Depending on this value the respective file is either excluded (True) or added (False).

例如,如果要排除所有以字母"a"开头的文件名,则可以执行以下操作...

For example, if you wanted to exclude all filenames beginning with the letter 'a', you'd do something like...

def exclude_function(filename):
    if filename.startswith('a'):
        return True
    else:
        return False

mytarfile.add(..., exclude=exclude_function)

对于您的情况,您想要类似...

For your case, you'd want something like...

EXCLUDE_FILES = ['README', 'INSTALL', '.cvsignore']

def exclude_function(filename):
    if filename in EXCLUDE_FILES:
        return True
    else:
        return False

mytarfile.add(..., exclude=exclude_function)

...可以简化为...

...which can be reduced to...

EXCLUDE_FILES = ['README', 'INSTALL', '.cvsignore']

mytarfile.add(..., exclude=lambda x: x in EXCLUDE_FILES)

更新

TBH,我不会过分担心过时警告,但是如果您想使用新的filter参数,则需要类似...

TBH, I wouldn't worry too much about the deprecation warning, but if you want to use the new filter parameter, you'd need something like...

EXCLUDE_FILES = ['README', 'INSTALL', '.cvsignore']

def filter_function(tarinfo):
    if tarinfo.name in EXCLUDE_FILES:
        return None
    else:
        return tarinfo

mytarfile.add(..., filter=filter_function)

...可以简化为...

...which can be reduced to...

EXCLUDE_FILES = ['README', 'INSTALL', '.cvsignore']

mytarfile.add(..., filter=lambda x: None if x.name in EXCLUDE_FILES else x)

这篇关于Python tarfile并排除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆