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

  • SEARCH
  • Home
  • 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 7446879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:27:18+00:00 2026-05-29T12:27:18+00:00

Possible Duplicate: Assignment Condition in Python While Loop What’s the equivalent of this: while

  • 0

Possible Duplicate:
Assignment Condition in Python While Loop

What’s the equivalent of this:

while (line = p.stdout.readline()) != '':
    ...

in Python?

I don’t like having to do this:

line = p.stdout.readline()
while line != '':
    sys.stdout.write(line)
    line = p.stdout.readline()

Although I’ve been using the latter for years… I suspect there is no alternative. I thought p.stdout supported iteration, like

for line in p.stdout:
    sys.stdout.write(line)

but unfortunately it doesn’t, unlike the handle returned from open() for example.

Edit: Sorry, I was wrong, it does support it, the problem is that I can’t get it out right away like I can with p.stdout.readline(). Adding sys.stdout.flush() doesn’t seem to help, so it must be getting buffered inside p.stdout.

  • 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-29T12:27:19+00:00Added an answer on May 29, 2026 at 12:27 pm

    You can accomplish this using the built-in function iter() using the two-argument call method:

    for line in iter(p.stdout.readline, ''):
        ...
    

    Documentation for this:

    iter(o[, sentinel])
    …
    If the second argument, sentinel, is given, then o must be a callable object. The iterator created in this case will call o with no arguments for each call to its next() method; if the value returned is equal to sentinel, StopIteration will be raised, otherwise the value will be returned.

    One useful application of the second form of iter() is to read lines of a file until a certain line is reached. The following example reads a file until the readline() method returns an empty string:

    with open('mydata.txt') as fp:
        for line in iter(fp.readline, ''):
            process_line(line)
    

    edit: Based on your edit it looks like your actual problem is related to buffering. Based on the context I am guessing you are using subprocess.Popen() with stdout=subprocess.PIPE. Instead of using the file handle p.stdout directly you should be p.communicate() to read the output of your subprocess.

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

Sidebar

Related Questions

Possible Duplicate: Javascript equivalent of PHP's list() In PHP you can do assignment like
Possible Duplicate: Python conditional assignment operator Apologies for such a simple question, but googling
Possible Duplicate: Ruby Assignment Syntax I don't get the concept of an assignment method
Possible Duplicate: Conditional operator assignment with Nullable<value> types? Hi, Why this doesn't work? DateTime?
Possible Duplicate: C# variable initializations vs assignment Just like in the title, could someone
Possible Duplicate: Why use two stacks to make a queue? I got this assignment
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: How do I do multiple assignment in MATLAB? When dealing with cell
Possible Duplicate: How do I do multiple assignment in MATLAB? So let's say I
Possible Duplicate: Why ‘this’ is a pointer and not a reference? Is there any

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.