Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取每个硬盘驱动器分区的名称和大小_Python_Python 3.x - Fatal编程技术网

Python 获取每个硬盘驱动器分区的名称和大小

Python 获取每个硬盘驱动器分区的名称和大小,python,python-3.x,Python,Python 3.x,我已经编写了代码,列出了我电脑上使用的所有驱动器号。如何获取每个驱动器的名称、总大小、可用空间和已使用空间?(我正在使用Python 3.7) 我的代码: import os import string drives = ['%s:' % drive + '\\' for drive in string.ascii_uppercase if os.path.exists('%s:' % drive)] 对于Python3.3及更高版本,您可以使用模块,该模块具有disk\u usage函数,

我已经编写了代码,列出了我电脑上使用的所有驱动器号。如何获取每个驱动器的名称、总大小、可用空间和已使用空间?(我正在使用Python 3.7)

我的代码:

import os
import string

drives = ['%s:' % drive + '\\' for drive in string.ascii_uppercase if os.path.exists('%s:' % drive)]

对于Python3.3及更高版本,您可以使用模块,该模块具有
disk\u usage
函数,返回一个命名元组,其中包含硬盘中的总空间、已用空间和可用空间量

import shutil

total, used, free = shutil.disk_usage("/")

print("Total: %d GiB" % (total // (2**30))
您也可以使用模块获取分区信息。
您必须安装模块(
pip install psutil

获得分区信息后,可以使用
disk\u usage
函数,该函数运行与
shutil
模块相同的脚本来获取每个分区的spcae信息

导入psutil
对于psutil.disk_分区()中的磁盘:
如果是disk.fstype:
打印(disk.device、psutil.disk\u用法(disk.mountpoint))

Hi hngchris,欢迎来到Stack Overflow。如果你让我们知道你得到了什么结果,你还尝试了什么,以及类似的细节,你会在这个问题上有更多的运气。