One Python script giving “user input” to another python script

一笑奈何 提交于 2021-02-18 07:41:05

问题


this is my first question, I hope I'm doing this right. let's say I have these this file:

"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:

"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? And what if the first script asks for input several times?

It's important that I CANNOT EDIT SIMPLE.PY, only the other file

Thanks in advance :)


回答1:


import sys
print "Hello",sys.argv[1]

running C:\Python26\python.exe simple.py Alice would produce

Hello Alice

There's a good example on how to get input from the system into a python application here:

  • http://www.tutorialspoint.com/python/python_command_line_arguments.htm

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":

C:> echo "Alice" | run simple.py




回答2:


For a case as simple as simple.py, you should be able to use 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. 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.

import pexpect

child = pexpect.spawn('python simple.py')
child.expect("your name?")
child.sendline('Alice')



回答3:


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.

  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'


来源:https://stackoverflow.com/questions/21875141/one-python-script-giving-user-input-to-another-python-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!