Windows错误(3, '系统找不到指定的路径')
我在下面这行代码上有时会遇到一个异常:
WindowsError(3, 'The system cannot find the path specified')
总共有大约1956个PDF文件(在之前定义的路径中),而这个异常是在其中43个文件上出现的。我没有发现这些出现异常的文件在路径和文件名上有什么规律。
有什么建议可以帮助我找出问题吗?
totalBytes = 0
if pdfFile.endswith(".pdf") and \
(" permit " in pdfFile or " Permit " in pdfFile):
filename = os.path.join(root, pdfFile)
try:
absolutePath = os.path.abspath(filename)
print ("absolutePath", absolutePath)
# exception on this line, occasionally:
numberOfBytes = os.path.getsize(absolutePath)
print ("numberOfBytes", numberOfBytes)
totalBytes += numberOfBytes
except WindowsError as windowsError:
print (windowsError, filename)
1 个回答
4
你可以用一个小技巧来绕过256个字符的限制:在绝对文件名的前面加上\\?\
。当然,要记得把这些斜杠转义成"\\\\?\\"
。
另外,你可以看看这个链接:命名文件、路径和命名空间。简单来说,以\\?\
开头的文件名会使用不同的解析方式,这样就有了不同的限制。