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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:53:28+00:00 2026-05-19T13:53:28+00:00

My python code spawns the child process, and it prints out messages both stdout

  • 0

My python code spawns the child process, and it prints out messages both stdout and stderr.
I need to print them differently.

I have the following code to spawn child process and get the stdout result from it.

cmd = ["vsmake.exe", "-f"]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
for line in iter(p.stdout.readline, ''):
    print line,
    sys.stdout.flush()
    pass
p.wait()

How can I modify the code to check if the child process prints out message through stderr also?

ADDED

I need to print out the stderr and stdout as soon as the child process prints out something. And it is cross platform implementation, so it should run on Mac/Linux/PC.

  • 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-05-19T13:53:29+00:00Added an answer on May 19, 2026 at 1:53 pm
    p = Popen(cmd, bufsize=1024,
    stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
    p.stdin.close()
    print p.stdout.read() #This will print the standard output from the spawned process
    print p.stderr.read() #This is what you need, error output <-----
    

    So basically the error output gets redirected to the stderr Pipe.

    If you need something more in real in time. I mean lines printed as soon as the spawned process prints something to stdout orstderr` then you can do something like:

    def print_pipe(type_pipe,pipe):
        for line in iter(pipe.readline, ''):
             print "[%s] %s"%(type_pipe,line),
    
    p = Popen(cmd, bufsize=1024,
    stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
    
    t1 = Thread(target=print_pipe, args=("stdout",p.stdout,))
    t1.start()
    t2 = Thread(target=print_pipe, args=("stderr",p.stderr,))
    t2.start()
    
    #optionally you can join the threads to wait till p is done. This is avoidable but it 
    # really depends on the application.
    t1.join()
    t2.join()
    

    In this case two threads will print every time that a line is written either to stdout or stderr. The parameter type_pipe just makes the distinction when the lines are printed to know if they are coming from stderr or stdout.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some old python code that uses the pywin32 extensions. Starting out with
If I have Python code class A(): pass class B(): pass class C(A, B):
I'm looking for Python code that removes C and C++ comments from a string.
I have the following Python code: import xml.dom.minidom import xml.parsers.expat try: domTree = ml.dom.minidom.parse(myXMLFileName)
I have some Python code that works correctly when I use python.exe to run
I have a method in my Python code that returns a tuple - a
I am interested in getting some Python code talking to some Ruby code on
I'm trying to run some python code under Apache 2.2 / mod_python 3.2.8. Eventually
I am interested in enabling code folding in Vim for Python code. I have
I found the bottleneck in my python code, played around with psycho etc. Then

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.