【问题标题】:Python Zipfile extractall IOError on Windows when extracting files from long paths从长路径中提取文件时,Windows 上的 Python Zipfile extractall IOError
【发布时间】:2017-03-18 02:15:22
【问题描述】:

我正在运行 python zipfile extractall,它提取到一个长度超过 255 个字符的路径。在 Windows 7 64 位上运行它。 我开始关注错误[Errno 2] No such file or directory: u'

有什么想法吗?

它是我要从中提取/提取的网络文件夹。所以我将文件夹挂载为网络驱动器 t:\ 暂时解决了这个问题。

【问题讨论】:

  • 首先确保文件存在。其次,尝试import os; os.getcwd() 并确保它为您提供的路径是相关文件所在的目录。
  • 文件存在,如果我使用上面提到的解决方法运行它,它就可以工作。

标签: python windows-7 zipfile


【解决方案1】:

这对我有用:

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 取自pathname too long to open?

【讨论】:

    猜你喜欢
    • 2012-07-10
    • 2015-06-15
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多