【问题标题】:python FileNotFoundError: [WinError 3] for getsize(filepath)python FileNotFoundError: [WinError 3] for getsize(filepath)
【发布时间】:2016-02-09 19:52:57
【问题描述】:

我创建了计算侧文件夹中文件大小总和的函数。提前我可以排除文件扩展名

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) 

上述方法返回文件夹中的文件计数/大小。 os.walk(path) 遍历所有文件和子文件夹

问题是一旦发现文件路径超过260个字符getsize(filepath) 给出如下错误

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

那么计算文件大小的最佳方法是什么。还有其他方法/库吗?

【问题讨论】:

    标签: python winapi windows-7 filenames python-3.4


    【解决方案1】:

    这是由于WinAPI在windows平台上的限制。任何超过 260 个字符的文件路径都必须以特殊方式处理。阅读此python win32 filename length workaround

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-22
      • 1970-01-01
      • 1970-01-01
      • 2018-10-16
      • 1970-01-01
      • 2020-06-03
      相关资源
      最近更新 更多