Python 查看服务器磁盘信息

  • 2020 年 1 月 11 日
  • 筆記

查看磁盘信息,主要用到了两个方法

  • psutil.disk_partitions()
  • psutil.disk_usage()

使用方法: 1、安装 psutil

pip install psutil

2、进入 python shell,并 import psutil

输入 help(psutil.disk_partitions)

返回

Help on function disk_partitions in module psutil:    disk_partitions(all=False)      Return mounted partitions as a list of      (device, mountpoint, fstype, opts) namedtuple.      'opts' field is a raw string separated by commas indicating mount      options which may vary depending on the platform.        If *all* parameter is False return physical devices only and ignore      all others.

输入 help(psutil.disk_usage)

返回

Help on function disk_usage in module psutil:    disk_usage(path)      Return disk usage statistics about the given *path* as a      namedtuple including total, used and free space expressed in bytes      plus the percentage usage.

3、应用

输入 psutil.disk_partitions()

>>> psutil.disk_partitions()  [sdiskpart(device='/dev/disk1', mountpoint='/', fstype='hfs', opts='rw,local,rootfs,dovolfs,journaled,multilabel'), sdiskpart(device='/dev/disk12s2', mountpoint='/Volumes/QQ', fstype='hfs', opts='ro,nosuid,quarantine,local,dovolfs,ignore-ownership,multilabel')]

输入 psutil.disk_usage(‘/’)

>>> psutil.disk_usage("/")  sdiskusage(total=120101666816, used=88392372224, free=31447150592, percent=73.8)

注:上述查到的大小信息单位为 bytes,因此,为了便于直观的观察使用量,应将其转换为M或G为单位的数据