Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python与命令行程序通信?_Python_Windows_Cmd_Subprocess_Communicate - Fatal编程技术网

如何使用python与命令行程序通信?

如何使用python与命令行程序通信?,python,windows,cmd,subprocess,communicate,Python,Windows,Cmd,Subprocess,Communicate,如何使用python传递cmd并与之通信 谢谢。(使用windows平台)请解释以下内容: 与进程交互:向stdin发送数据。从标准输出读取数据,然后 stderr,直到到达文件末尾。等待进程终止 communicate()在发送输入后阻塞,直到程序执行完毕。在您的示例中,程序在发送“1”后等待更多的输入,但Python在到达下一行之前等待它退出,这意味着整个过程都会死锁 如果您希望大量的读写可以互换,请转到stdin/stdout,然后向它们写入/读取数据。请解释以下内容: 与进程交互:向st

如何使用python传递cmd并与之通信

谢谢。(使用windows平台)

请解释以下内容:

与进程交互:向stdin发送数据。从标准输出读取数据,然后 stderr,直到到达文件末尾。等待进程终止

communicate()
在发送输入后阻塞,直到程序执行完毕。在您的示例中,程序在发送
“1”
后等待更多的输入,但Python在到达下一行之前等待它退出,这意味着整个过程都会死锁

如果您希望大量的读写可以互换,请转到
stdin
/
stdout
,然后向它们写入/读取数据。

请解释以下内容:

与进程交互:向stdin发送数据。从标准输出读取数据,然后 stderr,直到到达文件末尾。等待进程终止

communicate()
在发送输入后阻塞,直到程序执行完毕。在您的示例中,程序在发送
“1”
后等待更多的输入,但Python在到达下一行之前等待它退出,这意味着整个过程都会死锁


如果你想读和写很多东西可以互换,可以读到stdin/
stdout
和读/写到它们。

为什么不把接收器进程的相关部分粘贴到这里呢?可以尝试类似于
proc.stdin.write(data\u to\u write)的方法
为什么不将接收器进程的相关部分粘贴到此处?可以尝试类似于
过程标准写入(数据写入)
的方法,谢谢它帮助我现在了解下一步要做什么。@user1717522--如果您对您的问题找到满意的答案,您应该将其标记为已接受。谢谢,这有助于我现在了解下一步要做什么。@user1717522--如果您发现问题的答案令人满意,您应该将其标记为已接受。
import subprocess
import sys

proc = subprocess.Popen(["program.exe"], stdin=subprocess.PIPE) #the cmd program opens
proc.communicate(input="filename.txt") #here the filename should be entered (runs)
#then the program asks to enter a number:
proc.communicate(input="1") #(the cmd stops here and nothing is passed)
proc.communicate(input="2") # (same not passing anything)