擅长:python、mysql、java
<p>指定的度量单位是字节。您可以使用此代码将其转换为Gb的
当你使用这个值时,它会有一个尾随的“L”,但这并不影响计算。</p>
<pre><code>values=psutil.virtual_memory()
def get_human_readable_size(self,num):
exp_str = [ (0, 'B'), (10, 'KB'),(20, 'MB'),(30, 'GB'),(40, 'TB'), (50, 'PB'),]
i = 0
while i+1 < len(exp_str) and num >= (2 ** exp_str[i+1][0]):
i += 1
rounded_val = round(float(num) / 2 ** exp_str[i][0], 2)
return '%s %s' % (int(rounded_val), exp_str[i][1])
total_size = get_human_readable_size(values.total)
</code></pre>