简体   繁体   English

一个Python脚本为另一个python脚本提供“用户输入”

[英]One Python script giving “user input” to another python script

this is my first question, I hope I'm doing this right. 这是我的第一个问题,我希望我做得对。 let's say I have these this file: 假设我有这些文件:

"simple.py": “simple.py”:

a=raw_input("your name?")
print "Hello",a

but with a different script, I want to execute "simple.py" many time and giving the input automatically, that would work like: 但是使用不同的脚本,我想多次执行“simple.py”并自动输入,这样就可以了:

"everyone.py" “everyone.py”

run simple.py input=Alice
run simple.py input=Bob
...

to get "Hello Alice" "Hello Bob" ... 得到“你好爱丽丝”“你好鲍勃”......

I know it's possible to make "everyone.py" run "simple.py" by using os.system, but is there any practical way to do something like this? 我知道可以通过使用os.system使“everyone.py”运行“simple.py”,但是有什么实际的方法可以做这样的事情吗? And what if the first script asks for input several times? 如果第一个脚本多次请求输入怎么办?

It's important that I CANNOT EDIT SIMPLE.PY , only the other file 重要的是我不能编辑SIMPLE.PY ,只能编辑其他文件

Thanks in advance :) 提前致谢 :)

For a case as simple as simple.py , you should be able to use subprocess.Popen : 对于作为简单的情况下simple.py ,你应该能够使用subprocess.Popen

import subprocess

child = subprocess.Popen(['python', 'simple.py'], stdin=subprocess.PIPE)
child.communicate('Alice')

For more complex cases, the Pexpect module may be useful. 对于更复杂的情况, Pexpect模块可能很有用。 It helps automate interaction with normally-interactive programs by providing more convenient, robust interfaces to send input, wait for prompts, and read output. 它通过提供更方便,更健壮的接口来发送输入,等待提示和读取输出,从而有助于自动化与常规交互式程序的交互。 It should work in cases where Popen doesn't work or is more annoying. 它适用于Popen不工作或更烦人的情况。

import pexpect

child = pexpect.spawn('python simple.py')
child.expect("your name?")
child.sendline('Alice')
import sys
print "Hello",sys.argv[1]

running C:\\Python26\\python.exe simple.py Alice would produce 运行C:\\Python26\\python.exe simple.py Alice会产生

Hello Alice 你好爱丽丝

There's a good example on how to get input from the system into a python application here: 有一个关于如何从系统输入到python应用程序的好例子:

Since you didn't mention that you can not modify simple.py then you would need to automaticly input things into the raw_input() and the fastest way to do this is simply to pipe in data into the script as "input": 既然你没有提到你不能修改simple.py那么你就需要输入全自动东西进入 raw_input()并做到这一点,最快的方法就是简单地管数据到脚本为“输入”:

C:> echo "Alice" | C:>回声“爱丽丝”| run simple.py 运行simple.py

Unfortunately neither of the answers above worked for me so I came up with a third solution for others to try. 不幸的是,上面的答案都没有对我有用,所以我想出了第三个解决方案让其他人尝试。

To send inputs from one python file to another (python version 3.7), I used three files. 要将输入从一个python文件发送到另一个(python版本3.7),我使用了三个文件。

  1. File for running the subprocess 用于运行子进程的文件
  2. File for outputs (very simple) 输出文件(非常简单)
  3. File that needs the inputs 需要输入的文件

Here are the three files in the same order as above. 以下是与上述顺序相同的三个文件。

You don't need to print out the output, but I'll include the terminal output below the file examples. 您不需要打印输出,但我会在文件示例下面包含终端输出。 The subprocess file: 子进程文件:

from subprocess import Popen,PIPE

p1 = Popen(["python","output_file.py"], stdout=PIPE)
p2 = Popen(["python", "input_file.py"], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close()

output = p2.communicate()[0]

print(output)

The output file is very simple and there may be a way to work around it. 输出文件非常简单,可能有办法解决它。 Nevertheless, here is my version: 不过,这是我的版本:

print(1)
print(2)
print('My String')

The input file requires type casting for numbers. 输入文件需要对数字进行类型转换。

i = input('Enter a number: ')
j = input('Enter another: ')
k = int(i) + int(j)
print(k)
l = input('Tell me something. ')
print(l)

Here is the terminal output: 这是终端输出:

b'Enter a number: Enter another: 3\r\nTell me something. My String!\r\n'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM