我正在尝试使用 Python 获取硬盘驱动器大小和可用空间(我在 macOS 上使用 Python 2.7)。
我正在尝试使用
os.statvfs('/')
,尤其是使用以下代码。
我在做什么正确吗?我应该使用变量 giga
的哪个定义?
import os
def get_machine_storage():
result=os.statvfs('/')
block_size=result.f_frsize
total_blocks=result.f_blocks
free_blocks=result.f_bfree
# giga=1024*1024*1024
giga=1000*1000*1000
total_size=total_blocks*block_size/giga
free_size=free_blocks*block_size/giga
print('total_size = %s' % total_size)
print('free_size = %s' % free_size)
get_machine_storage()
编辑:
statvfs
在 Python 3 中被弃用,你知道任何替代方案吗?
注意:正如评论部分中的一些人提到的,此解决方案适用于Python 3.3 及更高版本。对于 Python 2.7,最好使用
psutil
库,它有一个 disk_usage
函数,包含有关 total、used 和 free 磁盘空间的信息:
import psutil
hdd = psutil.disk_usage('/')
print ("Total: %d GiB" % hdd.total / (2**30))
print ("Used: %d GiB" % hdd.used / (2**30))
print ("Free: %d GiB" % hdd.free / (2**30))
shutil
模块,它有一个 disk_usage
函数,返回一个命名元组,其中包含硬盘驱动器中的总空间、已用空间和可用空间。
您可以调用下面的函数并获取有关磁盘空间的所有信息:
import shutil
total, used, free = shutil.disk_usage("/")
print("Total: %d GiB" % (total // (2**30)))
print("Used: %d GiB" % (used // (2**30)))
print("Free: %d GiB" % (free // (2**30)))
输出:
Total: 931 GiB
Used: 29 GiB
Free: 902 GiB
https://pypi.python.org/pypi/psutil
import psutil
obj_Disk = psutil.disk_usage('/')
print (obj_Disk.total / (1024.0 ** 3))
print (obj_Disk.used / (1024.0 ** 3))
print (obj_Disk.free / (1024.0 ** 3))
print (obj_Disk.percent)
代码大致正确,但您使用了错误的字段,这可能会在不同的系统上给您带来错误的结果。正确的方法是:
>>> os.system('df -k /')
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/root 14846608 3247272 10945876 23% /
>>> disk = os.statvfs('/')
>>> (disk.f_bavail * disk.f_frsize) / 1024
10945876L
当您不知道如何处理函数的结果时,打印出类型会有所帮助。
print type(os.statvfs('/'))
返回<type 'posix.statvfs_result'>
这意味着它不是像字符串或 int 这样的内置类实例。
您可以使用
dir(instance)
检查您可以使用该实例做什么
print dir(os.statvfs('/'))
打印所有属性、函数、变量...
['__add__', '__class__', '__contains__', '__delattr__', '__doc__',
'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__',
'__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__',
'__lt__', '__mul__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__',
'__str__', '__subclasshook__', 'f_bavail', 'f_bfree', 'f_blocks',
'f_bsize', 'f_favail', 'f_ffree', 'f_files', 'f_flag', 'f_frsize',
'f_namemax', 'n_fields', 'n_sequence_fields', 'n_unnamed_fields']
通过访问其中一个变量,例如
os.statvfs('/').f_ffree
,您可以提取一个整数。
用
print type(os.statvfs('/').f_ffree)
仔细检查,
它确实打印<type 'int'>
.
根据此处的答案以 GiB 为单位显示磁盘大小的单行解决方案:
>>> import shutil
>>> [f"{y}: {x//(2**30)} GiB" for x, y in zip(shutil.disk_usage('/'), shutil.disk_usage('/')._fields)]
['total: 228 GiB', 'used: 14 GiB', 'free: 35 GiB']
本帖所有答案只提供根分区的磁盘大小。这是我为需要完整/整个硬盘大小的人编写的代码:
total = int()
used = int()
free = int()
for disk in psutil.disk_partitions():
if disk.fstype:
total += int(psutil.disk_usage(disk.mountpoint).total)
used += int(psutil.disk_usage(disk.mountpoint).used)
free += int(psutil.disk_usage(disk.mountpoint).free)
print(f'''
TOTAL DISK SPACE : {round(total / (1024.0 ** 3), 4)} GiB
USED DISK SPACE : {round(used / (1024.0 ** 3), 4)} GiB
FREE DISK SPACE : {round(free / (1024.0 ** 3), 4)} GiB
''')
我可以知道如何检查网络驱动程序存储状态