Get hard disk size in Python

放肆的年华 提交于 2019-12-20 09:54:13

问题


I am trying to get the hard drive size and free space using Python (I am using Python 2.7 with macOS).

I am trying with os.statvfs('/'), especially with the following code. Is it correct what I am doing? Which definition of the variable giga shall I use?

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()

EDIT: statvfs is deprecated in Python 3, do you know any alternative?


回答1:


Python provides you with the shutil module, which has a disk_usage function, returning a named tuple with the amounts of total, used and free space in your hard drive.

You can call the function as below and get all information about your disk's 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)))

Output:

Total: 931 GiB
Used: 29 GiB
Free: 902 GiB



回答2:


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)



回答3:


Printing out the type can help, when you don't know how to handle a function's result.

print type(os.statvfs('/')) returns <type 'posix.statvfs_result'>

That means it isn't a built in class instance like a string or int..

You can check what you can do with that instance with dir(instance)

print dir(os.statvfs('/')) prints all of it's the properties, functions, variables...

['__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']

By accessing one of the variables, like os.statvfs('/').f_ffree you can extract an integer.

Double check with print type(os.statvfs('/').f_ffree), it does print <type 'int'>.




回答4:


The code is about right, but you're using wrong fields, which may give you the wrong results on a different system. The correct way would be:

>>> 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


来源:https://stackoverflow.com/questions/48929553/get-hard-disk-size-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!