format函数

def memissue():
    print('内存信息:')
    mem = psutil.virtual_memory()
    # 单位换算为GB
    memtotal = mem.total / 1024 / 1024 / 1024
    memused = mem.used / 1024 / 1024 / 1024
    membaifen = mem.used / mem.total * 100
    print('总内存{:.2f}GB'.format(memtotal))
    print('已使用{:.2f}GB'.format(memused))
    print('内存使用率为:{}%'.format(format(membaifen, '.2f')))

输出:

内存信息:
总内存15.87GB
已使用7.70GB
内存使用率为:48.54%

'%.2f' %a

def cuplist():
    print('磁盘信息:')
    disk = psutil.disk_partitions()
    diskuse = psutil.disk_usage('/')
    #单位换算为GB
    diskused = diskuse.used / 1024 / 1024 / 1024
    disktotal = diskuse.total / 1024 / 1024 / 1024
    diskbaifen = diskused / disktotal * 100
    print('%.2fGB' % diskused)
    print('%.2fGB' % disktotal)
    print('%.2f' % diskbaifen)

 

相关文章:

  • 2022-12-23
  • 2022-01-10
  • 2022-02-07
  • 2021-11-24
  • 2021-10-19
  • 2021-09-16
  • 2021-12-25
猜你喜欢
  • 2022-01-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案