python获取磁盘使用(通用)

import psutil

def get_disk_info():
    content = ""
    for disk in psutil.disk_partitions():
        if 'cdrom' in disk.opts or disk.fstype == '':
            continue
        disk_name_arr = disk.device.split(':')
        disk_name = disk_name_arr[0]
        disk_info = psutil.disk_usage(disk.device)
        # 磁盘剩余空间,单位G
        free_disk_size = disk_info.free//1024//1024//1024
        # 当前磁盘使用率和剩余空间G信息
        info = "%s盘使用率:%s%%, 剩余空间:%iG\n" % (disk_name, str(disk_info.percent), free_disk_size)
        content = content + info
    print(content)
    return
get_disk_info()

  

posted @ 2021-03-10 11:06  奕呀!  阅读(768)  评论(0编辑  收藏  举报