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 :D
stdin. Why not use pipes instead?