open() and EOFError

Neil D. Cerutti neilc at norwich.edu
Tue Jul 8 10:29:18 EDT 2014


On 7/7/2014 7:10 PM, Mark Lawrence wrote:
> On 07/07/2014 23:09, Gregory Ewing wrote:
>> Marko Rauhamaa wrote:
>>>     with open(path) as f:
>>>         ...
>>>
>>> If the open() call is guarded against exceptions (as it usually should),
>>> one must revert to the classic syntax:
>>
>> Hmmm, maybe we could do with a with-except statement:
>>
>>     with open(path) as f:
>>        ...
>>     except IOError:
>>        # Catches exceptions in the with-expression only
>>        ...
>>
>> Although that would be a bit confusing.
>
> I wrap the with inside a try/except, the other file handling parts
> within another try/except and use the finer grained exceptions from PEP
> 3151 to write (at least to my eye) cleaner looking code.  Somehow I
> think we'll get agreement on the best way to do this when the cows come
> home.

On Windows it's my experience that EOF from interactive sessions is 
ignored. Programs keep going as best they can, providing some other 
means of exit, e.g., an 'exit' command.

But maybe that's just the shell.

-- 
Neil Cerutti





More information about the Python-list mailing list