now*_*wox 10 python shell perl
在 Perl 中,如果我想执行 shell 命令,例如foo,我会这样做:
#!/usr/bin/perl
$stdout = `foo`
Run Code Online (Sandbox Code Playgroud)
在 Python 中,我发现了这个非常复杂的解决方案:
#!/usr/bin/python
import subprocess
p = subprocess.Popen('foo', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout = p.stdout.readlines()
retval = p.wait()
Run Code Online (Sandbox Code Playgroud)
有没有更好的解决方案?
请注意,我不想使用callor os.system。我想stdout放在一个变量上
一个简单的方法是使用sh包。一些例子:
import sh
print(sh.ls("/"))
# same thing as above
from sh import ls
print(ls("/"))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
363 次 |
| 最近记录: |