468,287 Members | 1,898 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,287 developers. It's quick & easy.

Copying file descriptors

I am using the following code. It is used to spawn a new process (using
popen) and to change the file handles so that subsequent writes to
standard output with printf goes into this child process standard input.
import os

child_stdin = os.popen("cat -", "w")
old_stdout = os.dup(1)
os.close(child_stdin.fileno())

print "foo"

os.dup2(old_stdout, 1)
os.close(old_stdout)

It works. But the results is:
close failed: [Errno 9] Bad file descriptor
when I am using python 2.4. Note that python 2.3 did not output such an
error.

I am guessing that upon ending the script, python tries to close
child_stdin file object, whereas the underlying file descriptor has
already been closed. Thus the error.

Does anyone knows what to do to avoir this error message?

Thanks
Didier
Oct 6 '06 #1
0 926

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by François Pinard | last post: by
5 posts views Thread by John Marshall | last post: by
36 posts views Thread by Roman Mashak | last post: by
AdrianH
1 post views Thread by AdrianH | last post: by
5 posts views Thread by yinglcs | last post: by
2 posts views Thread by DJ Dharme | last post: by
reply views Thread by NPC403 | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.