[Tutor] Communicating Between Programs Using Raw Inputs

Steve Willoughby steve at alchemy.com
Wed Jun 15 00:54:20 CEST 2011


On 14-Jun-11 15:48, Steven D'Aprano wrote:

> Normally you would do this by redirecting standard input. What operating
> system are you using? In Linux, you would do something like:
>
>
> # run script foo.py taking input from the output of bar.py
> foo.py < bar.py

Actually, no, that will send the *source code* of bar.py as the input to 
foo.py.  I think you mean:

bar.py | foo.py

which also should work in DOS as well (although less efficiently).

> However, I don't know if this will actually work for raw_input. It may
> not. Try it and see.

It should.

>
> Perhaps a better way is to have your program accept a user name and
> password on the command line, and only prompt for them if not given.
> Then you can say:
>
> foo.py --user=fred --password="y8Mr3 at hzi"

This is one way.  Another would be to use the subprocess module in 
Python which will let one program invoke another program, and have a 
file-like object on which it can write data, which the child program 
will see as its standard input (and read in to raw_input).

-- 
Steve Willoughby / steve at alchemy.com
"A ship in harbor is safe, but that is not what ships are built for."
PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C


More information about the Tutor mailing list