Bad file descriptor

Roy Smith roy at panix.com
Thu Nov 13 19:41:22 EST 2014


In article <mailman.15802.1415923204.18130.python-list at python.org>,
 Ben Finney <ben+python at benfinney.id.au> wrote:

> satishmlmlml at gmail.com writes:
> 
> > import os
> > os.write(1, b'Hello descriptor world\n')
> > OSError: Bad file descriptor
> 
> It works fine for me::
> 
>     >>> import os
>     >>> os.write(1, b'Hello descriptor world\n')
>     Hello descriptor world
>     23
> 
> You don't say which Python, or which version, you're using. In the
> absence of different information, most of us will assume the latest
> stable release of CPython. Currently, that is CPython 3.4.

I don't think this has anything to do with which version of Python he's 
using.  I think all that's happening is (for some unknown reason), fd 1 
is closed before his program runs.  You can demonstrate this, for 
example, in bash:


$ cat hello.py 
import os
os.write(1, b'Hello descriptor world\n')


$ python hello.py 
Hello descriptor world


$ 1<&- python hello.py 
Traceback (most recent call last):
  File "hello.py", line 2, in <module>
    os.write(1, b'Hello descriptor world\n')
OSError: [Errno 9] Bad file descriptor


You owe the Oracle 5 minutes of his life back and a cookie for making 
him read enough of the bash manual to figure out the syntax to tell it 
to close a descriptor.



More information about the Python-list mailing list