尝试使用 zipfile 模块的 extractall 方法提取 zip 文件时出现以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\zipfile.py", line 964, in extractall
self.extract(zipinfo, path, pwd)
File "C:\Python27\lib\zipfile.py", line 952, in extract
return self._extract_member(member, path, pwd)
File "C:\Python27\lib\zipfile.py", line 996, in _extract_member
target = file(targetpath, "wb")
IOError: [Errno 2] No such file or directory: 'D:\\GITHUBDB\\C++\\FellowTraveler
_Open-Transactions\\FellowTraveler-Open-Transactions-846a939\\sample-data\\no_ba
skets\\client_data\\purse\\tBy5mL14qSQXCJK7Uz3WlTOKRP9M0JZksA3Eg7EnnQ1\\T1Q3wZWg
eTUoaUvn9m1lzIK5tn5wITlzxzrGNI8qtaV\\CvHGtfOOKzQKL5hFL7J4iF5yAodVKhS1rxPzME5R9XA'
发生这种情况是因为 Windows 对路径有 260 个字符的限制。 做 cd 不是一个选项,大部分路径来自存档本身。 我读过“\\?\”前缀,但将它添加到 extractall 参数不起作用,它在 zipfile 模块内的 os.mkdir 上失败。
用“\\?\”修复 zipfile.py 似乎是可行的,但却是一个丑陋的解决方案。
有什么想法吗?
最佳答案
在 Windows 上,有一种方法可以支持长文件路径。
只需确保您的路径是 unicode 绝对路径并添加 \\?\
前缀即可。例如:
zip = ZipFile(zip_filename)
zip.extractall(u'\\\\?\\D:\\a_long_path')
我已经在 python 2.7 上使用 ZipFile
类对其进行了测试,它运行良好。
您可以阅读有关 Windows 上文件命名的更多信息 here .
关于解压缩具有深层目录结构的存档时出现 Python IOError 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13113568/