[已解决]python FileNotFoundError: [WinError 3] for getsize(filepath)

时间:2022-07-25
本文章向大家介绍[已解决]python FileNotFoundError: [WinError 3] for getsize(filepath),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

问题代码:

def sourceStatic(path, exclude):

    # exclude list convert to lower 
    exclude = list(map(lambda x:x.lower(), exclude))
    files_size = 0
    files_count = 0
    for root, dirs, files in os.walk(path):
        for fileName in files:
            fname, fileEx = os.path.splitext(fileName)
            fileEx = (fileEx[1:]).lower()
            if not any(fileEx in item for item in exclude):
                print(fileName)
                filePath = os.path.join(root,fileName)
                fileSize = getsize(filePath)
                files_size += fileSize
                files_count += 1

    # return multiple data as dictionary              
    ret = {}
    ret['files_size'] = size(files_size)
    ret['files_size_byte'] = files_size
    ret['files_count'] = files_count
    print(ret)
    return (ret) 

报错:

  File "C:Python34libgenericpath.py", line 50, in getsize
    return os.stat(filename).st_size
FileNotFoundError: [WinError 3] The system cannot find the path specified:

解决:

filePath = u"\\?\" + filePath
fileSize = getsize(filePath)