Subprocess confusion: how file-like must stdin be?

Cameron Laird claird at lairds.us
Fri Aug 18 12:40:01 EDT 2006


In article <mailman.9508.1155911464.27775.python-list at python.org>,
Fredrik Lundh  <fredrik at pythonware.com> wrote:
>Cameron Laird wrote:
>
>> Your interactive session does indeed exhibit the behavior that
>> puzzles me.  My expectation was that StringIO and the std* 
>> parameters to Popen() were made for each other; certainly there
>> are many cases where stdout and stderr can be redirected *to* a
>> StringIO.  Is it simply the case that stdin demands a more
>> file-like object?  While that disappoints me, I certainly can
>> program around it.  My question, then:  does stdin effectively
>> require something really in the filesystem, or perhaps the
>> stdout of a previous subprocess?  Is there no built-in way to
>> feed it an in-memory construct?
>
>set the appropriate stream to subprocess.PIPE, and write to it.
>
>    p = subprocess.Popen(..., stdin=subprocess.PIPE)
>    p.stdin.write("hello")
>    p.stdin.close() # signal end of file
			.
			.
			.
Of course!  My; it's a sign of how far I was from the right
subprocess mentality that I so missed what is obvious after
the fact.  The source code (for the 2.4 release) also comes
close to making it clear, as I realized about the time you
posted your follow-up, Fredrik.

Is this--streaming data to a subprocess--too lightweight to
deserve write-up in the (online) Cookbook?  I'll volunteer.
In retrospect, I see there are plenty of examples on-line
of the usage, but I don't think an ignorant searcher (like
me) will find them on his own.

Thanks, Fredrik.



More information about the Python-list mailing list