WindowsError(3,'系统无法找到指定的路径')

时间:2014-04-22 20:52:04

标签: python windows python-3.x

我在有时下面的行中收到以下异常:

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 个答案:

答案 0 :(得分:4)

你应该能够通过一个奇怪的技巧来解决256个字符的限制:将\\?\添加到绝对文件名。当然要逃避这些斜线:"\\\\?\\"

另见:Naming Files, Paths, and Namespaces。 TL; DR,一个不同的文件名解析器用于以\\?\开头的具有不同限制的名称。

相关问题