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 7729423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:57:52+00:00 2026-06-01T05:57:52+00:00

I was looking to implement a python script that called another script and captured

  • 0

I was looking to implement a python script that called another script and captured its stdout. The called script will contain some input and output messages eg

print ("Line 1 of Text")
variable = raw_input("Input 1 :")
print "Line 2 of Text Input: ", vairable

The section of the code I’m running is

import subprocess

cmd='testfile.py'
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
so, se = p.communicate()
print(so)

The problem that is occurring is that the stdout is not printing until after the script has been executed. This leaves a blank prompt waiting for the user input. Is there a way to get stdout to print while the called script is still running?

Thanks,

  • 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-01T05:57:53+00:00Added an answer on June 1, 2026 at 5:57 am

    There are two problems here.

    1. Firstly, python is buffering output to stdout and you need to prevent this. You could insert a call to sys.stdout.flush() in testfile.py as Ilia Frenkel has suggested, or you could use python -u to execute testfile.py with unbuffered I/O. (See the other stack overflow question that Ilia linked to.)

    2. You need a way of asynchronously reading data from the sub-process and then, when it is ready for input, printing the data you’ve read so that the prompt for the user appears. For this, it would be very helpful to have an asynchronous version of the subprocess module.

    I downloaded the asynchronous subprocess and re-wrote your script to use it, along with using python -u to get unbuffered I/O:

    import async_subprocess as subprocess
    
    cmd = ['python', '-u', 'testfile.py']
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
    so = p.asyncread()
    print so,
    (so, se) = p.communicate()
    print so
    

    When I run this script using python -u I get the following results:

    $ python -u script.py
    Line 1 of Text
    Input 1:
    

    and the script pauses, waiting for input. This is the desired result.

    If I then type something (e.g. “Hullo”) I get the following:

    $ python -u script.py
    Line 1 of Text
    Input 1:Hullo
     Line 2 of Text Input:  Hullo
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best way to implement a python program that will take a
I have been looking to implement a custom class of : IList<ArraySegment<byte>> this will
I'm looking to implement content negotiation on some resources in a Rails app. I'm
I'm looking to implement the jQuery UI so that the slider bar fills one
I'm looking for C or Python code to implement either of the two pseudocode
I'm looking to write a Python import filter or preprocessor for source files that
I have written a Python script that currently is designed to handle traditional CGI
I was looking for a portable script or command line program that can synchronize
Totally new to python, I'm actually working on an ex-colleague's script. in looking at
I'm looking for a very simple way to implement fibers in Python. I'm sure

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.