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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:16:14+00:00 2026-05-27T21:16:14+00:00

I have a python program which takes input from stdin. Now, I’ve to write

  • 0

I have a python program which takes input from stdin. Now, I’ve to write another program and call it as a sub process so that every time I start this subprocess it should read data from another text file and write it to stdin and then my main program reads it from stdin and uses it.

out.py:

from subprocess import Popen, PIPE, STDOUT

import os

import sys

def read():
    p = Popen(['python', 'in.py'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)

    out = p.communicate()

    print out[0]

def main():
    read()

if __name__ == "__main__":
    main()

in.py:

import sys,os

import subprocess

def main():

    f = open("ad.txt","r")

    for line in f:
        print line

if __name__ == "__main__":
    main()

Basically my problem here is the in.py reads the whole file and prints into STDOUT which I don’t want and rather it should communicate one character once. If the ad.txt is like :

asdfgh

I should get "a" then "s" then "d" then "f" .. so on.. one by one character everytime I call the function read the next character from the file should be read. That’s the thing! Phew!! Please help me and I got a heck of a work to do! Thanks in advance 😀

  • 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-27T21:16:15+00:00Added an answer on May 27, 2026 at 9:16 pm

    The first program should read from a file (or stdin) and write to stdout (not stdin). Similarly, the second program should read from stdin and write to stdout. Now you can glue them together in the command line using the pipe symbol:

    python first.py | python second.py
    

    That’s it! None of the programs has to be aware of the other; that’s the beauty of using “pipelining”.

    Addendum: Shell pipelining works over a buffer. The shell listens to the first program and fills the buffer with its output. Simultaneously, the second program reads from the buffer as long as there is something to read, otherwise it waits idly. The communication is simultaneous and requires only a fixed size of memory.

    Example code:

    # first.py
    import sys
    for line in open("input.txt"):
        processed_line = process_line(line)
        sys.stdout.write(processed_line)
    
    # second.py
    import sys
    for line in sys.stdin:
        processed_line = process_line(line)
        sys.stdout.write(processed_line)
    

    These examples work over lines (which is better if you are working with text files). You could easily do the same byte by byte.

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

Sidebar

Related Questions

So right now I have written a python program which takes the path name
I have a program which during it's run sometimes needs to call python in
I have a Python program which is going to take text files as input.
I have a source of python program which doesn't have any documentation or comments.
I have a python program, which runs on the CPython implementation, and inside it
I have a small Python program, which uses a Google Maps API secret key.
I have been trying to create a simple program with Python which uses OpenCV
I have a program in python that includes a class that takes a function
I have a python program that calls into a c++ library, which wishes to
I have a Python program that sends keystrokes to another application using SendKeys. Some

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.