python subprocess and shlex

2.4k Views Asked by At

I am trying to get the following python code to work, I am just not able to get the syntax to work. Can someone assist?

import subprocess
import shlex

subprocess.call(shlex.split('top -d 5 -n 1 -b -o %CPU | head -n 12 > output.txt'))
1

There are 1 best solutions below

0
On BEST ANSWER

I got the below to work, Thanks for help!

import subprocess

subprocess.Popen("top -d 5 -n 1 -b -o %CPU | head -n 12 > output.txt",shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)