计算c驱动器大小时,python访问被拒绝

问题描述 投票:1回答:1

我正在使用以下函数来使用Python计算目录的大小

def get_path_size(path):
    total_size = 0
    for path, dirs, files in os.walk(path):
        for f in files:
            fp = os.path.join(path, f)
            total_size += os.path.getsize(fp)

    return total_size

我已经用许多目录测试了它,但是当我尝试计算C盘的大小时它不起作用

print(get_path_size("C:/"))

OSError:[WinError 1920]系统无法访问该文件:'C:/ Users \ asus \ AppData \ Local \ Microsoft \ WindowsApps \ MicrosoftEdge.exe'

如何在没有管理员权限的情况下计算目录的大小?

python directory filesize access-denied
1个回答
1
投票

在特定的情况下,你寻找驱动器的用法,我建议你使用shutil

import shutil
shutil.disk_usage("C:")

产量:

usage(total=498439548928, used=204051705856, free=294387843072)

请注意,GUI中报告的值存在轻微差异,因为驱动器上的保留空间未考虑在内,因此可能不适合您的需要。

© www.soinside.com 2019 - 2024. All rights reserved.