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

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

问题描述

我的问题是如下,我会用简单的例子来说明吧。我写了需要用户交互的python脚本,特别是它使用的raw_input()函数来获取用户的输入。下面的code简单地要求用户在两个号码连续型(每间按回车键),然后返回答案(惊讶,惊讶的是,它被称为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的模块来写这个剧本,具体使用'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")

这code不工作,并在执行时返回错误:

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(输入=无)

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的,但期望遇到了问题,像它不输出的孩子code的输入请求,我只是一般具有不知道它是什么做的。

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.

推荐答案

您只能叫沟通一次。因此,你需要在一次通过全部输入,即 child.communicate(1 \\ N1 \\ n)。或者你可以写信给标准输入:

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

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

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

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

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