测试系统为:Centos 6.7
Python版本为: 3.6.4
脚本功能:常用单位的转换,这里用内存来模拟
import pstuil def bytes2human(n): symbols = (‘K‘,‘M‘,‘G‘,‘T‘,‘P‘,‘E‘,‘Z‘,‘Y‘) prefix = {} for i,s in enumerate(symbols): prefix[s] = 1 << (i + 1) * 10 for s in reversed(symbols): if n >= prefix[s]: value = float(n) / prefix[s] return ‘%.1f%s‘ % (value,s) return ‘%sB‘ % n if __name__ == ‘__main__‘: bytes2human(psutil.virtual_memory().total)
原文地址:https://www.cnblogs.com/dachenzi/p/8232861.html
时间: 2024-10-10 03:54:10