0

我想使用 Popen 执行命令,请参见下面的代码:

import subprocess
p = subprocess.Popen('/usr/bin/python a.py')
p2 = subprocess.Popen('ls', stdout = subprocess.PIPE)
print p2.stdout.readlines()

当我执行此脚本时,我收到以下错误:

File "demo.py", line 4, in <module>
  p0 = subprocess.Popen('/usr/bin/python a.py')
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
 errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child
  raise child_exception
OSError: [Errno 2] No such file or directory

有什么问题?另一个问题是,如果 a.py 是一个“while-True-loop”,p2 可以返回吗?

谢谢!

4

1 回答 1

4

检查您的 python 和“a.py”脚本路径并确保它们存在!

之后试试这个:

import subprocess
p = subprocess.Popen(["/usr/bin/python" , "a.py"], stdout=subprocess.PIPE)
out, err = p.communicate()

祝你好运!

于 2013-09-08T07:00:55.273 回答