【问题标题】:Unable to display any output无法显示任何输出
【发布时间】:2016-04-07 00:59:48
【问题描述】:

伙计们,我是 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)

【问题讨论】:

  • sys_info 已定义但从未调用,因此没有可打印的内容

标签: python python-2.7


【解决方案1】:

如果 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 系统详细信息,您可能需要通过
pip install wmi 安装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相关的个别属性

【讨论】:

    【解决方案2】:

    你需要调用你写的函数。

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-26
      • 2016-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多