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

Christopher Arndt chris.arndt at web.de
Tue Jan 9 15:26:07 CET 2007


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


More information about the Tutor mailing list