Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8762763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:36:57+00:00 2026-06-13T15:36:57+00:00

The problem I have is as follows, and I will use simple example to

  • 0

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

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

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)

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…

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:36:58+00:00Added an answer on June 13, 2026 at 3:36 pm

    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")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm facing a problem that I can summarize as it follows: I have a
I have a dll written in C++ that I want to export to Python
Newbie here! My problem is as follows: I have a dynamically populating ul where
I have a mysql problem, my query looks as follows but not complete SELECT
My problem is as follows. I have an array of objects in the form
I've seen that a few instances of this problem have been raised already. However,
Problem: I have a table that prints out vertical but I would like it
The problem is simple: I have triggers and generators in my Firebird 2.1.4 database
I have a simple WPF window that has 12 buttons on it. I want
I have a fairly simple sync problem. I have a table with about 10

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.