打开 .exe 并通过 Python 子进程将命令传递给它? [英] Opening an .exe and passing commands to it via Python subprocess?

查看:92
本文介绍了打开 .exe 并通过 Python 子进程将命令传递给它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我打开了我的 .exe 程序,但我想从我的 python 脚本中将字符串传递给它.我是这样打开exe的

So I have my .exe program that opens but i want to pass strings to it from my python script. Im opening the exe like this

import subprocess
p = subprocess.Popen("E:\Work\my.exe", shell=True)
#let user fill in some tables
p.communicate("userInfo")

我想将一个字符串传递给这个程序,同时让它在后台运行而不接管任何想法?

I want to pass a string to this program while having it just run in the background and not take over any ideas?

推荐答案

来自 使用 subprocess 模块的 Python 文档:

From the Python documentation for Using the subprocess module:

你不需要 shell=True 来运行一个批处理文件,也不需要运行一个基于控制台的可执行文件.

You don’t need shell=True to run a batch file, nor to run a console-based executable.

来自 Popen 对象的 Python 文档 :

From the Python documentation for Popen Objects :

注意,如果要将数据发送到进程的stdin,则需要使用 stdin=PIPE 创建 Popen 对象.同样,要得到任何东西除了结果元组中的 None 之外,您需要给出 stdout=PIPE和/或 stderr=PIPE 也是.

Note that if you want to send data to the process’s stdin, you need to create the Popen object with stdin=PIPE. Similarly, to get anything other than None in the result tuple, you need to give stdout=PIPE and/or stderr=PIPE too.

代码示例:

from subprocess import Popen, PIPE

p = Popen(r"E:\Work\my.exe", stdin=PIPE)
p.communicate("userInfo")

这篇关于打开 .exe 并通过 Python 子进程将命令传递给它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆