Search code examples
pythonsubprocessshlex

python subprocess and shlex


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

Solution

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