在 Python 中获取硬盘大小
Get hard disk size in Python
我正在尝试使用 Python 获取硬盘驱动器大小和免费 space(我在 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 2直到Python 3.3
注意:正如评论部分提到的一些人,此解决方案适用于Python 3.3及更高版本.对于Python 2.7最好使用psutil
library, which has a disk_usage
函数,包含total,的信息使用和免费磁盘space:
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))
Python 3.3及以上:
对于 Python 3.3 及更高版本,您可以使用 shutil
module, which has a disk_usage
函数,返回一个命名元组,其中包含硬盘驱动器中的总数量、已用数量和空闲数量 space。
您可以如下调用该函数并获取有关您磁盘的所有信息 space:
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
当您不知道如何处理函数的结果时,打印出类型会有帮助。
print type(os.statvfs('/'))
returns <type 'posix.statvfs_result'>
这意味着它不是内置的 class 实例,例如字符串或整数..
您可以使用 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'>
.
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
根据此处的答案以 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']
本帖所有答案只提供根分区的磁盘大小。这是我为需要 complete/entire 硬盘大小的人编写的代码:
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
''')
我正在尝试使用 Python 获取硬盘驱动器大小和免费 space(我在 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 2直到Python 3.3
注意:正如评论部分提到的一些人,此解决方案适用于Python 3.3及更高版本.对于Python 2.7最好使用psutil
library, which has a disk_usage
函数,包含total,的信息使用和免费磁盘space:
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))
Python 3.3及以上:
对于 Python 3.3 及更高版本,您可以使用 shutil
module, which has a disk_usage
函数,返回一个命名元组,其中包含硬盘驱动器中的总数量、已用数量和空闲数量 space。
您可以如下调用该函数并获取有关您磁盘的所有信息 space:
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
当您不知道如何处理函数的结果时,打印出类型会有帮助。
print type(os.statvfs('/'))
returns <type 'posix.statvfs_result'>
这意味着它不是内置的 class 实例,例如字符串或整数..
您可以使用 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'>
.
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
根据此处的答案以 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']
本帖所有答案只提供根分区的磁盘大小。这是我为需要 complete/entire 硬盘大小的人编写的代码:
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
''')