在一个脚本中使用 Python 的子进程和 Popen 来运行另一个需要用户交互的 Python 脚本(通过 raw_input) [英] Using Python's subprocess and Popen in one script to run another Python script which requires user interaction (by raw_input)

查看:24
本文介绍了在一个脚本中使用 Python 的子进程和 Popen 来运行另一个需要用户交互的 Python 脚本(通过 raw_input)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题如下,我用简单的例子来说明.我写了一个需要用户交互的python 脚本,特别是它使用raw_input() 函数来获取用户的输入.下面的代码只是要求用户连续输入两个数字(在每个数字之间按回车键),并返回答案(惊喜,惊喜,它被称为sum_two_numbers.py").哼哼!

The problem I have is as follows, and I will use simple example to illustrate it. I have written a python script that requires user interaction, specifically it uses the raw_input() function to get the user's input. The code below simply asks the user to type in two numbers in succession (hitting enter between each), and returns the answer (surprise, surprise, it is called 'sum_two_numbers.py'). Ho-hum!

#! /usr/bin/python

#  -------------------
#  sum_two_numbers.py
#  -------------------
#  This script asks the user for two numbers and returns the sum!

a = float(raw_input("Enter the first number:"))
b = float(raw_input("Enter the second number:"))

print a+b

现在,我想编写一个单独的 python 脚本来执行上述脚本并向其提供"两个必要的数字.因此,我将此脚本称为feeder.py".我尝试使用 Python 的subprocess"模块编写此脚本,特别是使用Popen"类及其相关的通信"方法.下面是尝试输入数字5"和4"的脚本.

Now, I want to write a separate python script that executes the above script and 'feeds' the two necessary numbers to it. I hence call this script 'feeder.py'. I tried to write this script using Python's 'subprocess' module, specifically using the 'Popen' class and its associated 'communicate' method. Below is the script trying to feed the numbers '5' and '4'.

#! /usr/bin/python

#  ----------
#  feeder.py
#  ----------
import subprocess

child = subprocess.Popen("./sum_two_numbers.py",stdin=subprocess.PIPE)

child.communicate("5")
child.communicate("4")

此代码不起作用,并在执行时返回错误:

This code doesn't work, and returns the errors upon execution:

$ ./feeder.py
Enter the first number:Enter the second number:Traceback (most recent call last):
  File "./sum_two_numbers.py", line 6, in <module>
    b = float(raw_input("Enter the second number:"))
EOFError: EOF when reading a line
Traceback (most recent call last):
  File "./feeder.py", line 8, in <module>
    child.communicate("4")
  File "/usr/lib/python2.7/subprocess.py", line 740, in communicate
    self.stdin.write(input)
ValueError: I/O operation on closed file

我不知道如何编写 'feeder.py' 以便它可以做我想做的事,这些错误一直阻碍着我.我怀疑这个错误是由于文档中的以下注释引起的:

I'm don't know how to write the 'feeder.py' so that it will do what I want, these errors keep hindering me. I suspect that this error arises because of the following comment in the documentation:

Popen.communicate(input=None)

Popen.communicate(input=None)

与进程交互:将数据发送到标准输入.从 stdout 和 stderr 读取数据,直到到达文件尾.等待进程终止.

Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate.

我不知道如何理解这句话,以及它如何帮助我...

I'm not sure what to make of this sentence, and how it can help me...

任何人都可以帮助我使上述脚本工作,即如何正确使用子进程和 Popen ......或者只是如何编写馈线"脚本 - 使用任何(不太晦涩的)语言!我尝试过 Pexpect、Expect,但遇到了一些问题,比如它不输出子代码的输入请求,而我通常不知道它在做什么.

Can anyone help me with making the above script work i.e. how to use subprocess and Popen properly... Or just how to write a 'feeder' script - in any (not too obscure) language! I have tried Pexpect, Expect but ran into problems like it not outputting the the child code's requests for input and me just generally having no idea what its doing.

推荐答案

communicate 只能调用一次.因此,您需要一次传递所有输入,即 child.communicate("1 1 ").或者,您可以写入标准输入:

You can only call communicate once. Therefore you need to pass all the input at once, i.e. child.communicate("1 1 "). Alternatively you can write to stdin:

child = subprocess.Popen("./test.py", stdin=subprocess.PIPE)         

child.stdin.write("1
")                                                       
child.stdin.write("1
")

这篇关于在一个脚本中使用 Python 的子进程和 Popen 来运行另一个需要用户交互的 Python 脚本(通过 raw_input)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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