如何在 Python 3 中与外部程序交互? [英] How to interact with an external program in Python 3?

查看:38
本文介绍了如何在 Python 3 中与外部程序交互?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Python 3,我想执行一个外部程序,通过在标准输入中提供一些文本来与之交互,然后打印结果.

Using Python 3, I want to execute an external program, interact with it by providing some text into standard input, and then print the result.

例如,我创建了以下外部程序,名为 test.py:

As an example, I created the following external program, called test.py:

print('Test Program')
print('1 First option, 2 Second Option')

choice = input()

if choice == '1':
    second_param = input('Insert second param: ')
    result = choice + ' ' + second_param
        
    print(result)

如果我直接运行这个程序,它会按预期工作.如果我提供输入 12,结果是 1 2.

If I run this program directly, it works as expected. If I provide the input 1 and then 2, the result is 1 2.

我想在另一个脚本中运行这个程序并与之交互以打印相同的结果.

I want to run this program in another script and interact with it to print the same result.

阅读subprocess 的文档后,并查看了关于 SO 的类似问题,我得到了以下结果:

After reading the documentation for subprocess, and checking out similar questions on SO, I ended up with the following:

EXTERNAL_PROG = 'test.py'

p = Popen(['py', EXTERNAL_PROG], stdout=PIPE, stdin=PIPE, shell=True)

print(p.stdout.readline().decode('utf-8'))
print(p.stdout.readline().decode('utf-8'))
p.stdin.write(b'1\n')
p.stdin.write(b'2\n')
print(p.stdout.readline().decode('utf-8'))

然而,当我运行代码时,程序在打印1 First option, 2 Second Option后冻结,我需要重新启动我的shell.这可能是由于 subprocess.stdout.readline() 期望找到一个换行符,而第二个参数的提示不包含换行符.

However, when I run the code, the program freezes after printing 1 First option, 2 Second Option, and I need to restart my shell. This is probably caused by the fact that subprocess.stdout.readline() expects to find a newline character, and the prompt for the second param doesn’t contain one.

我发现了 2 个 SO 问题,它们讨论了类似的问题,但我无法让它发挥作用.

I found 2 SO questions that talk about something similar but I couldn’t get it to work.

这里,答案推荐使用pexpect 模块.我尝试根据我的情况调整代码,但没有奏效.

Here, the answer recommends using the pexpect module. I tried to adapt the code to my situation but it didn’t work.

这里,建议使用-u,但添加它并没有改变任何东西.

Here, the suggestion is to use -u, but adding it didn’t change anything.

我知道可以通过修改 test.py 找到解决方案,但在我的情况下这是不可能的,因为我需要使用另一个外部程序,这只是基于它的一个最小示例.

I know that a solution can be found by modifying test.py, but this is not possible in my case since I need to use another external program and this is just a minimal example based on it.

推荐答案

如果你的程序输入是固定的(意味着输入在运行时不会改变),那么这个解决方案可能是相关的.

If you have fixed input to your program (means input not changing at run time) then this solution can be relevant.

首先创建文件.

  • 输入文件.将其命名为 input.txt 并将 1 2 放入其中

command = "python test.py < input.txt > output.txt 2>&1"

# now run this command

os.system(command)

当你运行它时,你会在同一个目录中找到output.txt.如果您的程序成功执行,则 output.txt 包含代码 test.py 的输出,但如果您的代码出现任何错误,则错误位于 output.txt.

When you run this, you will find output.txt in same directory. If your program is executed successfully then output.txt contains output of code test.py but if your code gives any error then error is in output.txt.

main.py 变成

import sys
from subprocess import PIPE, Popen

EXTERNAL_PROG = 'test.py'

p = Popen(['python3', EXTERNAL_PROG], stdout=PIPE, stdin=PIPE, stderr=PIPE)

print(p.stdout.readline())
print(p.stdout.readline())
p.stdin.write(b'1\n')
p.stdin.write(b'2\n')
p.stdin.flush()
print(p.stdout.readline())
print(p.stdout.readline())

这篇关于如何在 Python 3 中与外部程序交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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