[Tutor] feeding data to subprocess exes and getting results without writing files

Barton David David.Barton at nottingham.ac.uk
Thu Jan 11 10:00:34 CET 2007


Thanks Chris,

I figured it out after a while..

import subprocess
inputtext="my input string"
process=subprocess.Popen("myprog.exe -i stdin -o
stdout",stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.S
TDOUT)
outputtext,errortext=process.communicate(inputtext)
 
..so it is fairly simple after all.

Thanks for the help,
Dave

(ps I inadvertantly replied directly to you Chris, rather than the
mailing list- sorry-, so this is just for posterity in case anybody else
wants to know the solution)


-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On
Behalf Of Christopher Arndt
Sent: 09 January 2007 14:26
To: Tutor at python.org
Subject: Re: [Tutor] feeding data to subprocess exes and getting results
without writing files

Barton David schrieb:
> I just can't wrap my head around stdin, stdout and the whole pipes 
> thing, but there's got to be a relatively simple way to do this,
surely?

You have to distinguish between three different concepts:

1) file names
2) file objects
3) file contents

1) Is just a string with some identifier (the file path). To use a file
with that identifier in Python, you have to create a file object from it
by using the builtin 'open' function.

2) File objects are builtin Python objects that are usually created by
the 'open' function or returned by some other function. There are a few
file objects that are already opened and accessible to your Python
program. These are sys.stdin, sys.stderr and sys.stdout. They are file
objects, *not* strings representing the (non-existant) file name or file
content!

3) File contents are just represented by binary strings in Python. You
read/write them with the appropriate methods of file objects.

==> The subprocess.Popen constructor expects *file objects* not strings
for its 'stdin' and 'stdout' arguments. Read the documentation of
subprocess.Popen very carefully again (ignore references to file
*descriptors*).

BTW: a pipe is just an object or function that reads from one file
object and writes to another.

Chris
_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor

This message has been checked for viruses but the contents of an attachment
may still contain software viruses, which could damage your computer system:
you are advised to perform your own checks. Email communications with the
University of Nottingham may be monitored as permitted by UK legislation.



More information about the Tutor mailing list