[英]How to continually communicate with same process with Python Popen()
I am new at this so hope you understand... 我是新来的,希望您能理解...
Now I am using python subprocess module for sending the specific command. 现在,我正在使用python子进程模块发送特定命令。
After I send the command1 with Popen function, I would like to send a command2 to same process one more. 使用Popen函数发送command1之后,我想再次将command2发送到同一进程。
Is it possible...? 可能吗...?
// Example
// command1 -> Opening command for text program.
// command2 -> Executing the write and save command.(on above text program)
popen = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
// next....?
please let me know If I understand it incorrectly. 如果我理解不正确,请告诉我。
Check this out. 看一下这个。 In unix, we can execute multiple commands in one go like
在Unix中,我们可以一次性执行多个命令,例如
cmd_1;cmd_2
We will use same thing with sub-process. 我们将在子流程中使用相同的东西。
Make sure to use shell=True
. 确保使用
shell=True
。
import subprocess
all_cmd = "echo a;echo b;echo c"
p = subprocess.Popen(all_cmd,stdout=subprocess.PIPE, shell=True)
p_stdout,p_err = p.communicate()
print p_stdout.strip()
It is perfectly doable but you might want to look into something like pexpect
which offers more control over the dialog. 这是完全可行的,但您可能希望研究像
pexpect
这样的东西,它可以提供对对话框的更多控制。
cmd = subprocess.Popen(['command1'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # no shell=True here!
stdout, stderr = cmd.communicate('command2')
This presumes that command1
will keep running and reading additional commands on its standard input. 这假定
command1
将继续运行并在其标准输入上读取其他命令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.