Python 查看服务器磁盘信息

版权声明:本文为大事龙原创文章,未经允许不得转载。 https://blog.csdn.net/w_yunlong/article/details/78907979

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

  • 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为单位的数据

猜你喜欢

转载自blog.csdn.net/w_yunlong/article/details/78907979
今日推荐