无法显示任何输出

时间:2016-04-06 16:17:26

标签: python python-2.7

伙计我是Python的新手,但我正在尝试使用Python获取我的Windows 8信息。但我无法这样做,我无法找到以下代码的输出它没有显示任何内容。我正在使用Netbeans。

    # To change this license header, choose License Headers in Project Properties.
# To change this template file, choose Tools | Templates
# and open the template in the editor.
if __name__ == "__main__":

    import os
    import re

def sys_info():
    values  = {}
    cache   = os.popen2("SYSTEMINFO")
    source  = cache[1].read()
    sysOpts = ["Host Name", "OS Name", "OS Version", "Product ID", "System Manufacturer", "System Model", "System type", "BIOS Version", "Domain", "Windows Directory", "Total Physical Memory", "Available Physical Memory", "Logon Server"]

    for opt in sysOpts:
        values[opt] = [item.strip() for item in re.findall("%s:\w*(.*?)\n" % (opt), source, re.IGNORECASE)][0]
    return values
    if values == null :
        print "yep"
        print (values)

2 个答案:

答案 0 :(得分:0)

首先,如果importfunctionmain不按顺序排列您的展示位置。

这是正确的序列

import os
import re    

def SysInfo():
    values  = {}
    cache   = os.popen2("SYSTEMINFO")
    source  = cache[1].read()
    sysOpts = ["Host Name", "OS Name", "OS Version", "Product ID", "System Manufacturer", "System Model", "System type", "BIOS Version", "Domain", "Windows Directory", "Total Physical Memory", "Available Physical Memory", "Logon Server"]

    for opt in sysOpts:
        values[opt] = [item.strip() for item in re.findall("%s:\w*(.*?)\n" % (opt), source, re.IGNORECASE)][0]
    return values


if __name__ == "__main__":
    SysInfo()

即使在此之后,代码也没有生成任何输出。我windows 2010python 2.7。我想知道它是否与OS version有关。我不精通Windows,所以不能回答。

但是,如果您的目标是获取Windows系统详细信息,则可能需要安装wmi模块 pip install wmi

然后使用以下代码获取详细的系统信息或单个属性。

import wmi

x = wmi.WMI()    
systeminfo = x.Win32_ComputerSystem()[0]

print systeminfo
Manufarturer = systeminfo.Manufacturer
Model = systeminfo.Model

print "Manufarturer =", Manufarturer
print "Model =", Model

输出:

不包含print systeminfo

的详细信息
Manufarturer = Dell Inc.
Model = Latitude E5430 non-vPro

您可以挖掘与Win32_ComputerSystem class here

相关的各个属性

答案 1 :(得分:0)

您需要调用您编写的函数。

import os
import re

if __name__ == "__main__":    
    def sys_info():
        values  = {}
        cache   = os.popen2("SYSTEMINFO")
        source  = cache[1].read()
        sysOpts = ["Host Name", "OS Name", "OS Version", "Product ID", "System Manufacturer", "System Model", "System type", "BIOS Version", "Domain", "Windows Directory", "Total Physical Memory", "Available Physical Memory", "Logon Server"]

        for opt in sysOpts:
            values[opt] = [item.strip() for item in re.findall("%s:\w*(.*?)\n" % (opt), source, re.IGNORECASE)][0]
        return values
        if values == null :
            print "yep"
            print (values)

    # Now call the function here
    sys_info()