[Tutor] Unwanted "close failed in file object destructor" after broken pipe

Evert Rol evert.rol at gmail.com
Tue Apr 17 11:14:21 CEST 2012


> First, generate a file named "data.txt" as follows:
> 
> -- 8< ----------------------------------
> for i in range(1,100):
>    print '.' * 80
> -- 8< ----------------------------------
> 
> After that, save the following snippet as "test.py" in the directory
> containing "data.txt":
> 
> -- 8< ----------------------------------
> import sys
> 
> object_file = open('data.txt', 'rb')
> 
> for line in object_file:
>    sys.stdout.write(line)
> -- 8< ----------------------------------
> 
> Finally, run the following Unix shell command in the containing directory:
> 
> -- 8< ----------------------------------
> python test.py | head
> -- 8< ----------------------------------
> 
> At the end of printing some lines, the following error is reported:
> 
> -- 8< ----------------------------------
> close failed in file object destructor:
> Error in sys.excepthook:
> 
> Original exception was:
> -- 8< ----------------------------------
> 
> How to suppress that unwanted error message?

It's a problem with how the Python interpreter handles a broken pipe while shutting down. I.e., head terminates before Python has cleaned up the IO.
A search shows it as a bug report here: http://bugs.python.org/issue11380, and a good explanation here http://www.velocityreviews.com/forums/t749747-help-with-a-piping-error.html
The latter suggest using sys.stdout.flush() at the end. It may still throw an exception, but it then throws the proper exception (IOError: [Errno 32] Broken pipe), which you can wrap in a try-except block.
In fact, with sys.stdout.flush(), I don't see any error.
Note that on my Mac, btw, I don't have the problem (with or without the flush): possibly that either head or IO in general is behaving slightly different there.


Hope that helps,

  Evert

> After all doing something like the following does not give such an error:
> 
> -- 8< ----------------------------------
> cat data.txt | head
> -- 8< ----------------------------------

> Thank you very much.
> 
> -- 
> Best regards,
> Eus (FSF member #4445)
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list