[Python-Dev] PEP 389: argparse - new command line parsing module

Hrvoje Niksic hrvoje.niksic at avl.com
Wed Oct 7 13:26:59 CEST 2009


Paul Moore wrote:
> Traceback (most recent call last):
>   File "hello.py", line 13, in <module>
>     main()
>   File "hello.py", line 7, in main
>     sys.stdout.flush()
> IOError: [Errno 9] Bad file descriptor
> 
> (Question - is it *ever* possible for a Unix program to have invalid
> file descriptors 0,1 and 2? At startup - I'm assuming anyone who does
> os.close(1) knows what they are doing!)

Of course; simply use the >&- pseudo-redirection, which has been a 
standard sh feature (later inherited by ksh and bash, but not csh) for 
~30 years.  The error message is amusing, too:

$ python -c 'print "foo"' >&-
close failed in file object destructor:
Error in sys.excepthook:

Original exception was:


Adding an explicit flush results in a more understandable error message:

Traceback (most recent call last):
   File "<string>", line 1, in <module>
IOError: [Errno 9] Bad file descriptor


More information about the Python-Dev mailing list