Python tarfile extractall匹配字符串的文件除外

2024-04-20 05:16:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个遗留脚本,它通过python脚本获取boost库,然后提取并构建它们。在

在windows上,提取步骤失败,因为路径对于boost归档文件中的某些文件太长。E、 g

IOError: [Errno 2] No such file or directory: 'C:\\<my_path>\\boost_1_57_0\\libs\\geometry\\doc\\html\\geometry\\reference\\spatial_indexes\\boost__geometry__index__rtree\\rtree_parameters_type_const____indexable_getter_const____value_equal_const____allocator_type_const___.html'

有没有简单地使tarfile库extractall而忽略所有扩展名为.html的文件?在

或者,是否有一种方法允许路径超过windows限制266?在


Tags: 文件no路径脚本windowshtmltype步骤
1条回答
网友
1楼 · 发布于 2024-04-20 05:16:13

您可以遍历tar中的所有文件,只提取那些不以“.html”结尾的文件
导入操作系统 导入tarfile

def custom_files(members):
    for tarinfo in members:
        if os.path.splitext(tarinfo.name)[1] != ".html":
            yield tarinfo

tar = tarfile.open("sample.tar.gz")
tar.extractall(members=custom_files(tar))
tar.close()

示例代码和有关模块的信息已找到here

为了克服文件名大小的限制,请参阅Microsoft文档](https://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx

相关问题 更多 >