The os.system()
function returns the exit code of the shell, not the output from the shell command that did the work. To capture this output, you need to open a pipe and read from it. The way to do this within the os
module is to use os.popen()
instead of os.system()
os.popen("""lscpu | grep 'CPU(s):' | head -1 | awk '{print $2}'""").read()
Another way is to use the newer subprocess
module instead. Since os.popen()
has been depreciated since version 2.6, you might prefer subprocess
, especially if you expect your code to survive the next couple of Python revisions.
subprocess.getoutput(""""lscpu | grep 'CPU(s):' | head -1 | awk '{print $2}'""")
Side note: My triple quotes may not be strictly necessary here, but I like to put them into such calls, just to make sure they don't interfere with any quotes inside of any shell commands.
Good luck!