Capture and parse output from external command inside Python -


though new in python,i wrote list of array , want print list of array of bios information? how execute external command in script , capture output , parse it.

below code have written execute dmidecode | less command using os.popen() , store output variable called package:

#!/usr/bin/python import os f = os.popen("dmidecode | less") package = f.read() print 'bios information is:',package 

after executing above code:sudo python sample.py => output has follows:

bios information     vendor: *****     version: 1.40     release date: 09/07/2009     rom size: 1024 kb     characteristics:             pci supported             bios upgradeable             bios shadowing allowed             boot cd supported             selectable boot supported             bios rom socketed             edd supported             japanese floppy nec 9800 1.2 mb supported (int 13h)             japanese floppy toshiba 1.2 mb supported (int 13h)             5.25"/360 kb floppy services supported (int 13h)             5.25"/1.2 mb floppy services supported (int 13h)             3.5"/720 kb floppy services supported (int 13h)             3.5"/2.88 mb floppy services supported (int 13h)             8042 keyboard services supported (int 9h)             cga/mono video services supported (int 10h)             acpi supported             usb legacy supported             targeted content distribution supported 

so want parse value of:vendor , release date & version , should provide related values.

the question have execute external command in above script,capture output , parse it?
can me find out problem?

help me out have browse none document available...

you have 2 questions. pun 1 question per stackoverflow question. addresses first one.

to capture stdout or stderr output program can use subprocess.check_output().

"run command arguments , return output."

example:

  import subprocess    output = subprocess.check_output("dmidecode")   print(output.decode("utf-8"))  # convert bytes string 

when have output available can ask question how parse it, , want parsed data.


Comments