python captures the output instance of the shell script

  • 2020-05-19 05:10:09
  • OfStack

import subprocess
output =Popen(["mycmd","myarg"], stdout=PIPE).communicate()[0]


import subprocess
p = subprocess.Popen(['ls','-a'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
print out

# work on Unix/Linux only

import commands
print commands.getstatusoutput('wc -l file')[1]


Related articles: