我需要一种在linux,Windows和OS X上使用python来确定磁盘卷上剩余空间的方法。我目前正在解析各种系统调用(df,dir)的输出来实现这一点--有没有更好的方法?
发布于 2015-04-29 20:43:17
使用pip install psutil
安装psutil。然后,您可以使用以下命令获取可用空间量(字节):
import psutil
print(psutil.disk_usage(".").free)
发布于 2013-10-12 17:20:43
您可以在windows上使用wmi模块,在unix上使用os.statvfs。
对于窗口
import wmi
c = wmi.WMI ()
for d in c.Win32_LogicalDisk():
print( d.Caption, d.FreeSpace, d.Size, d.DriveType)
适用于unix或linux
from os import statvfs
statvfs(path)
发布于 2018-02-01 12:52:31
如果您运行的是python3:
使用带有os.path.realpath('/')
名称正则化的shutil.disk_usage()
:
from os import path
from shutil import disk_usage
print([i / 1000000 for i in disk_usage(path.realpath('/'))])
或
total_bytes, used_bytes, free_bytes = disk_usage(path.realpath('D:\\Users\\phannypack'))
print(total_bytes / 1000000) # for Mb
print(used_bytes / 1000000)
print(free_bytes / 1000000)
为您提供总空间、已用空间和可用空间( MB )。
https://stackoverflow.com/questions/51658
复制相似问题