在从长路径中提取文件时,Python Zipfile extractall 在Windows上会出现IOError错误

4

我正在运行Python zipfile extractall,它正在解压到一个超过255个字符的路径。在Windows 7 64位上运行此操作时,我遇到了以下错误:[Errno 2] No such file or directory: u'

有任何想法吗?

我想从/到网络文件夹中提取。因此,我将该文件夹挂载为网络驱动器T:\,这暂时解决了问题。


首先确保文件存在。其次,尝试使用 import os; os.getcwd() 命令,并确保它返回的路径是包含所需文件的目录。 - naiveai
文件已存在,如果我使用上述解决方法运行它,它可以正常工作。 - Atti
1个回答

9

这对我起作用:

class ZipfileLongPaths(zipfile.ZipFile):

    def _extract_member(self, member, targetpath, pwd):
        targetpath = winapi_path(targetpath)
        return zipfile.ZipFile._extract_member(self, member, targetpath, pwd)

winapi_path是指:

def winapi_path(dos_path, encoding=None):
    path = os.path.abspath(dos_path)

    if path.startswith("\\\\"):
        path = "\\\\?\\UNC\\" + path[2:]
    else:
        path = "\\\\?\\" + path 

    return path  

winapi_path来源于路径名过长无法打开?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接