使用 zipfile 在跳过列表中的文件时归档目录内容

Using zipfile to archive directory contents while skipping files from list

我正在使用 zipfile 创建目录中所有文件的存档(递归地,同时保留包括空文件夹在内的目录结构)并希望进程跳过列表中指定的文件名。

这是os.walks通过目录并将所有包含的文件和目录添加到存档的基本功能。

def zip_dir(path):
    zipname = str(path.rsplit('/')[-1]) + '.zip'
    with zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED) as zf:
        if os.path.isdir(path):
            for root, dirs, files in os.walk(path):
                for file_or_dir in files + dirs:
                    zf.write(os.path.join(root, file_or_dir),
                            os.path.relpath(os.path.join(root, file_or_dir),
                            os.path.join(path, os.path.pardir)))
        elif os.path.isfile(filepath):
            zf.write(os.path.basename(filepath))
    zf.printdir()
    zf.close()

我们可以看到代码也应该有处理单个文件的能力,但它主要是我们感兴趣的关于目录的部分。

现在假设我们有一个文件名列表,我们不想将其添加到 zip 存档中。

skiplist = ['.DS_Store', 'tempfile.tmp']

实现此目标的最佳和最简洁的方法是什么?

我尝试使用 zip 这有点成功,但由于某种原因导致它排除空文件夹(应包括空文件夹)。我不确定为什么会这样。

skiplist = ['.DS_Store', 'tempfile.tmp']
for root, dirs, files in os.walk(path):
    for (file_or_dir, skipname) in zip(files + dirs, skiplist):
        if skipname not in file_or_dir:
            zf.write(os.path.join(root, file_or_dir),
                    os.path.relpath(os.path.join(root, file_or_dir),
                    os.path.join(path, os.path.pardir)))

看看是否有人有添加跳过特定文件扩展名的能力的聪明想法也很有趣,也许像 .endswith('.png') 但我不完全确定如何将它与现有的跳过列表。

对于有关该功能的任何其他一般性评论以及它是否确实按预期正常工作以及任何优化或改进建议,我也将不胜感激。

您可以简单地检查文件是否不在 skiplist:

skiplist = {'.DS_Store', 'tempfile.tmp'}

for root, dirs, files in os.walk(path):
    for file in files + dirs:
        if file not in skiplist:
            zf.write(os.path.join(root, file),
                     os.path.relpath(os.path.join(root, file),
                     os.path.join(path, os.path.pardir)))

这将确保 skiplist 中的文件不会添加到存档中。

另一个优化是使 skiplist 成为一个集合,以防万一它变得非常大,并且您想要常数时间 O(1) 查找而不是使用列表的线性 O(N) 查找。

您可以在 TimeComplexity 对此进行更多研究,它显示了对数据结构的各种 Python 操作的时间复杂度。

关于扩展,可以使用os.path.splitext()提取扩展,逻辑同上:

from os.path import splitext

extensions = {'.png', '.txt'}

for root, dirs, files in os.walk(path):
    for file in files:
        _, extension = splitext(file)
        if extension not in extensions:
            zf.write(os.path.join(root, file),
                     os.path.relpath(os.path.join(root, file),
                     os.path.join(path, os.path.pardir)))

如果你想结合以上的特性,那么你可以分别处理文件和目录的逻辑:

from os.path import splitext

extensions = {'.png', '.txt'}
skiplist = {'.DS_Store', 'tempfile.tmp'}

for root, dirs, files in os.walk(path):
    for file in files:
        _, extension = splitext(file)
        if file not in skiplist and extension not in extensions:
            zf.write(os.path.join(root, file),
                     os.path.relpath(os.path.join(root, file),
                     os.path.join(path, os.path.pardir)))

    for directory in dirs:
        if directory not in skiplist:
            zf.write(os.path.join(root, directory),
                     os.path.relpath(os.path.join(root, directory),
                     os.path.join(path, os.path.pardir))) 

注意: 上面的代码片段不能单独使用,您需要编入当前代码才能使用这些想法。