系统内存和硬盘字节舍入,最接近的偶数千兆字节。Python

2024-10-03 19:23:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在收集当前机器的系统信息。部分信息是RAM和硬盘容量。问题是正在收集的容量是以字节而不是GB来度量的。

简而言之,我如何将内部规格的显示转换为与您从消费者/商业立场所看到的相似?在

1000GB HDD或8GB RAM,而不是可用的确切字节数。特别是对于不同的显卡,可以使用1024个不同数量的显卡。。。下面是我当前代码的一个示例:

import os
import wmi #import native powershell functionality
import math

c = wmi.WMI()  
SYSINFO = c.Win32_ComputerSystem()[0]    # Manufacturer/Model/Spec blob

RAMTOTAL = int(SYSINFO.TotalPhysicalMemory)        # Gathers only the RAM capacity in bytes.
RAMROUNDED = math.ceil(RAMTOTAL / 2000000000.) * 2.000000000        # attempts to round bytes to nearest, even, GB.
HDDTOTAL = int(HDDINFO.size) # Gathers only the HDD capacity in bytes.
HDDROUNDED = math.ceil(HDDTOTAL / 2000000000.) * 2.000000000        # attempts to round bytes to nearest, even, GB.
HDDPRNT = "HDD: " + str(HDDROUNDED) + "GB"
RAMPRNT = "RAM: " + str(RAMROUNDED) + "GB"
print(HDDPRNT)
print(RAMPRNT)


感兴趣的区域是8-11其中我将向上四舍五入到最接近的偶数,因为RAM/HDD的内部大小总是低于前面提到的原因。StackOverflow posts给我的这个方法是最准确的,在大多数机器上,但它仍然是硬编码的。这意味着硬盘驱动器只能精确到几百GB或数千GB,而不是两者兼而有之。而且,RAM也不是100%准确。在

我想到了几个解决方法,它们将产生我想要的结果:

  • RAMTOTAL添加可能可用或不可用的其他命令。允许输出GB而不是KB。然而。我希望它是WMI导入的一部分,而不是直接的原生Windows代码。

  • 一种静态修约法。即:if HDDTOTAL > 1TB四舍五入到小数点Xelse HDDTOTAL < 1TB使用不同的取整方法。


Tags: to方法代码import机器信息bytesmath
3条回答

我综合了前面两个建议。在

我使用了if循环,而不是while循环,但是得到了相同的结果。我还镜像了建议的powershell命令的相同内部过程,以使脚本更适合python,并且不添加模块/依赖项。在

GBasMB = int(1000000000) # Allows for accurate Bytes to GB conversion

global RAMSTRROUNDED
RAMTOTAL = int(SYSINFO.TotalPhysicalMemory) / (GBasMB) # Outputs GB by dividing by previous MB variable
RAMROUNDED = math.ceil(RAMTOTAL / 2.) * 2 # Rounds up to nearest even whole number
RAMSTRROUNDED = int(RAMROUNDED) # final converted variable

HDDTOTAL = int(HDDINFO.size) / (GBasMB) # Similar process as before for HardDrive
HDDROUNDED = math.ceil(HDDTOTAL / 2.) * 2 # round up to nearest even whole number

def ROUNDHDDTBORGB(): # function for determining TB or GB sized HDD
    global HDDTBORGBOUTPUT
    global HDDPRNT
    if HDDROUNDED >= 1000: # if equal to or greater than 1000GB, list as 1TB
        HDDTBORGB = HDDROUNDED * .001
        HDDTBORGBOUTPUT = str(HDDTBORGB) + "TB"
        HDDPRNT = "HDD: " + str(HDDTBORGBOUTPUT)
        print(HDDPRNT)
    elif HDDROUNDED < 1000: # if less than 1000GB list as GB
        HDDTBORGBOUTPUT = str(str(HDDROUNDED) + "GB")
        HDDPRNT = "HDD: " + str(HDDTBORGBOUTPUT)

我已经在几十台计算机上运行了这个脚本,似乎准确地收集了适当数量的RAM和HDD容量。不管集成显卡决定消耗和/或在硬盘上保留扇区等。。。在

我想你可以写一个简单的函数来解决它。如果以kB为单位的数字会明显变小或变大,我添加了一个不同后缀的可能性(它的灵感来自于一本书DiveintoPython3中非常相似的例子)。它可能看起来像这样:

def round(x):
    a = 0
    while x > 1000:
        suffixes = ('kB','MB','GB','TB')
        a += 1 #This will go up the suffixes tuple with each division
        x = x /1000
    return math.ceil(x), suffixes[a]

此函数的结果可能如下所示:

^{pr2}$

你可以像这样在你的代码中实现它:

import os
import wmi #import native powershell functionality
import math

def round(x):
    a = 0
    while x > 1000:
        suffixes = ('kB','MB','GB','TB')
        a += 1 #This will go up the suffixes tuple for each division
        x = x /1000
    return math.ceil(x), suffixes[a]

    .
    .
    .

RAMROUNDED = round(RAMTOTAL)       #attempts to round bytes to nearest, even, GB.
HDDTOTAL = int(HDDINFO.size) # Gathers only the HDD capacity in bytes.
HDDROUNDED = round(HDDTOTAL)        #attempts to round bytes to nearest, even, GB.
HDDPRNT = "HDD: " + str(HDDROUNDED[0]) + HDDROUNDED[1]
RAMPRNT = "RAM: " + str(RAMROUNDED[0]) + RAMROUNDED[1]
print(HDDPRNT)
print(RAMPRNT)

PowerShell内置了很多非常强大的原生数学功能,允许我们做一些事情,比如除以1GB,得到特定驱动器的整数(以千兆字节为单位)。在

因此,要查看总物理内存四舍五入1 GB,请执行以下操作:

get-wmiobject -Class Win32_ComputerSystem | 
select @{Name='Ram(GB)';Expression={[int]($_.TotalPhysicalMemory /1GB)}}

此方法称为计算属性,它与使用常规select语句(如select TotalPhysicalMemory)的不同之处在于,我告诉PowerShell进行一个新的Prop调用Ram(GB),并使用以下表达式来确定它的值。在

^{pr2}$

我使用的表达式在括号中开始,我得到TotalPhysicalMemory(返回值为17080483840)。然后除以1GB得到15.9074401855469。最后,我应用[int]将整件事情转换为一个整数,也就是说,使它成为一个整数,并适当地四舍五入。在

这是输出

>Ram(GB)
   -
 16

相关问题 更多 >