虽然我是python新手,但我写了数组列表,我想打印bios信息的数组列表?如何执行此脚本中的外部命令,并捕获输出并对其进行解析。
下面是我编写的代码,使用os.popen()执行dmidecode | less命令,并将其输出存储到名为package的变量中:
#!/usr/bin/python
import os
f = os.popen("dmidecode | less")
package = f.read()
print 'Bios Information is:',package执行上述代码后:sudo python sample.py =>输出如下:
BIOS Information
Vendor: *****
Version: 1.40
Release Date: 09/07/2009
ROM Size: 1024 kB
Characteristics:
PCI is supported
BIOS is upgradeable
BIOS shadowing is allowed
Boot from CD is supported
Selectable boot is supported
BIOS ROM is socketed
EDD is supported
Japanese floppy for NEC 9800 1.2 MB is supported (int 13h)
Japanese floppy for Toshiba 1.2 MB is supported (int 13h)
5.25"/360 kB floppy services are supported (int 13h)
5.25"/1.2 MB floppy services are supported (int 13h)
3.5"/720 kB floppy services are supported (int 13h)
3.5"/2.88 MB floppy services are supported (int 13h)
8042 keyboard services are supported (int 9h)
CGA/mono video services are supported (int 10h)
ACPI is supported
USB legacy is supported
Targeted content distribution is supported所以现在我想要解析一个值:供应商,发布日期和版本,它应该提供相关的值。
问题是,我必须执行上面脚本中的外部命令,捕获输出并解析它?
所以有人能帮我找出这个问题吗?
帮我一下,我已经浏览过了,但是没有可用的文档...
发布于 2015-09-26 18:50:38
你有两个问题。每个StackOverflow问题只有一个问题。这只解决了第一个问题。
要捕获程序的stdout或stderr输出,可以使用subprocess.check_output()。
“使用参数运行命令并返回其输出。”
示例:
import subprocess
output = subprocess.check_output("dmidecode")
print(output.decode("utf-8")) # Convert from bytes to string当您有了可用的输出时,您可以询问如何解析它,以及您想要对解析的数据做什么。
https://stackoverflow.com/questions/32794557
复制相似问题