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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:22:42+00:00 2026-06-17T11:22:42+00:00

How can I write to stdout from Python and feed it simultaneously (via a

  • 0

How can I write to stdout from Python and feed it simultaneously (via a Unix pipe) to another program? For example if you have

# write file line by line
with open("myfile") as f:
  for line in f:
    print line.strip()

But you’d like that to go line by line to another program, e.g. | wc -l so that it outputs the lines in myfile. How can that be done? 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-17T11:22:45+00:00Added an answer on June 17, 2026 at 11:22 am

    If you want to pipe python to wc externally, that’s easy, and will just work:

    python myscript.py | wc -l
    

    If you want to tee it so its output both gets printed and gets piped to wc, try man tee or, better, your shell’s built-in fancy redirection features.

    If you’re looking to run wc -l from within the script, and send your output to both stdout and it, you can do that too.

    First, use subprocess.Popen to start wc -l:

    wc = subprocess.Popen(['wc', '-l'], stdin=subprocess.PIPE)
    

    Now, you can just do this:

    # write file line by line
    with open("myfile") as f:
      for line in f:
        stripped = line.strip()
        wc.stdin.write(stripped + '\n')
    

    That will have wc‘s output go to the same place as your script’s. If that’s not what you want, you can also make its stdout a PIPE. In that case, you want to use communicate instead of trying to get all the fiddly details right manually.

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

Sidebar

Related Questions

How can I write data to stdout instead of writing file? I'm using GPG
how can i write a string (eg. Hello) to stdout from the stack? without,
I am trying to run a shell from my python program. I have used
I am grabbing an update from SVN via Python. I have this: try: output
how can I write DIRECTLY into console, without bothering with stdout in c#? I'm
I'm calling pipe.communicate from Python's subprocess module from Python 2.6. I get the following
I have that code: #!/usr/bin/python -u localport = 9876 import sys, re, os from
I have the following python code: import pty import subprocess os=subprocess.os from subprocess import
On Linux, I'm trying to redirect stdout from a console application to a file
I have a program that I run from the command line that looks like

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.