我正在努力使用python的子流程。我的任务是:
我到目前为止所做的尝试:
一。我被困在这里用波本。我明白如果我用
subprocess.call("put command here")
这有效。我想尝试使用类似的东西:
import subprocess
def run_command(command):
p = subprocess.Popen(command, shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
在这里我使用run_command("insert command here")
但这不起作用。
关于2。我认为答案应该和这里类似: Running shell command from Python and capturing the output, 但我不能得到1。为了工作,我还没试过。
至少要真正启动子流程,必须告诉Popen对象真正通信。
您可以查看Pexpect,这是一个专门为与基于shell的程序交互而设计的模块。
例如,启动scp命令并等待密码提示:
关于Python 3版本,请参见Pexpect-u。
相关问题 更多 >
编程相关推荐