popen(2) wierdness - help!
professor ned
ned at stromkern.com
Fri May 17 11:59:58 EDT 2002
Hi all,
this seems like it should be awfully simple, but the only answer I can
get from anyone seems to be "use Expect!" which, somehow, isn't very
satisfying.
Consider the following snippet of code (Python 2.2 running on RedHat
7.2, although Solaris 8 exhibits similar behavior):
---
#!/usr/local/bin/python
import os
cmd = "cat -"
stuff = "Hello, hello, hello...\n"
writeHandle = os.popen( cmd, 'w')
print "writeHandle is %s" % writeHandle
print "stuff is %s" % stuff
writeHandle.write( stuff )
writeHandle.close()
writeHandle, readHandle = os.popen2( cmd )
print "\nwriteHandle is %s " % writeHandle
print "readHandle is %s " % readHandle
print "stuff is %s " % stuff
writeHandle.write( stuff )
writeHandle.close()
readHandle.close()
---
Now, consider the output:
[ned at nmi-redhat72-dev ned]$ ./ptest.py
writeHandle is <open file 'cat -', mode 'w' at 0x810e9f0>
stuff is Hello, hello, hello...
Hello, hello, hello...
writeHandle is <open file '(fdopen)', mode 'w' at 0x8145138>
readHandle is <open file '(fdopen)', mode 'r' at 0x810d140>
stuff is Hello, hello, hello...
[ned at nmi-redhat72-dev ned]$ cat: write error: Broken pipe
---
Or the Solaris 8 version:
[ned]:~$ ./ptest.py
writeHandle is <open file 'cat -', mode 'w' at 0x103940>
stuff is Hello, hello, hello...
Hello, hello, hello...
writeHandle is <open file '(fdopen)', mode 'w' at 0x13b320>
readHandle is <open file '(fdopen)', mode 'r' at 0x13b7d0>
stuff is Hello, hello, hello...
[ned]:~$ cat: output error (0/23 characters written)
Broken pipe
For a single popen(), with the mode specified, you get file 'cat -'
instead of the more generice '(fdopen)' - although that doesn't seem
to much matter. But for popen2(), where the modes are also specified,
you get this broken pipe error, which ... makes no sense. Can the
writeHandle of popen2() not accept a string? Does it need a stream
from somewhere? All examples I've seen of this (which actually work)
have the input to the writeHandle be something like "ls -l".
Maddening. Any insight would be appreciated.
ned
More information about the Python-list
mailing list