In Perl, if I want to execute a shell command such as foo
, I'll do this:
#!/usr/bin/perl
$stdout = `foo`
In Python I found this very complex solution:
#!/usr/bin/python
import subprocess
p = subprocess.Popen('foo', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout = p.stdout.readlines()
retval = p.wait()
Is there any better solution ?
Notice that I don't want to use call
or os.system
. I would like to place stdout
on a variable
An easy way is to use sh
package. some examples:
import sh
print(sh.ls("/"))
# same thing as above
from sh import ls
print(ls("/"))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句