在 Python 中获取硬盘大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48929553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:54:39  来源:igfitidea点击:

Get hard disk size in Python

pythonmacospython-2.7hard-drive

提问by Nisba

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

我正在尝试使用 Python 获取硬盘驱动器大小和可用空间(我在 macOS 上使用 Python 2.7)。

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

我正在尝试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()

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

编辑: statvfs在 Python 3 中已弃用,您知道有其他选择吗?

回答by Vasilis G.

For Python 2 till Python 3.3

对于 Python 2 到 Python 3.3



Notice: As a few people mentioned in the comment section, this solution will work for Python 3.3and above. For Python 2.7it is best to use the psutillibrary, which has a disk_usagefunction, containing information about total, usedand freedisk space:

注意:正如评论部分中提到的一些人,此解决方案适用于Python 3.3及更高版本。对于Python 2.7,最好使用该psutil库,它有一个disk_usage函数,包含有关磁盘空间、已磁盘空间和可用磁盘空间的信息:

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 and above:

Python 3.3 及更高版本:

For Python 3.3 and above, you can use the shutilmodule, which has a disk_usagefunction, returning a named tuple with the amounts of total, used and free space in your hard drive.

对于 Python 3.3 及更高版本,您可以使用该shutil模块,该模块具有一个disk_usage函数,返回一个命名元组,其中包含硬盘驱动器中的总空间、已用空间和可用空间。

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

回答by Fausto Branco

https://pypi.python.org/pypi/psutil

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)

回答by lenik

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

回答by Hadus

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

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

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

这意味着它不是像 string 或 int 那样的内置类实例。

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

你可以检查你可以用这个实例做什么 dir(instance)

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

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

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

通过访问其中一个变量,就像os.statvfs('/').f_ffree您可以提取一个整数一样。

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

仔细检查print type(os.statvfs('/').f_ffree),它确实会打印<type 'int'>