[Tutor] How to handle try and except in this case
Dave Angel
d at davea.name
Tue Nov 29 16:48:00 CET 2011
On 11/29/2011 10:35 AM, Mic wrote:
>
>
> -----Ursprungligt meddelande----- From: Dave Angel Sent: Tuesday,
> November 29, 2011 4:25 PM To: Mic Cc: tutor at python.org Subject: Re:
> [Tutor] How to handle try and except in this case
>> (You top-posted again. Try to put your remarks AFTER the part you're
>> quoting, so that the message is self-descriptive)
>
> <SNIP - a perfectly reasonable message>
>
> Is that top posting?
>
No, you're absolutely right. I don't know what I was looking at. Maybe
I had your message scrolled when I was reading it. I am sorry.
>> The os.path.exists(filename) returns a boolean, and doesn't tie up
>> resources, so you are welcome to use it in an if expression.
>
>> if not (os.path.exists(file1) and os.path.exists(file2)):
> >print "hi"
>
>
> Thank you! What do you mean with that it "doesn't tie up resources"?
If you do multiple opens in the same expression, you can't easily*
capture all the file objects, especially in the case of an exception.
So there may be files left open that will get closed an indeterminate
amount of time in the future. That sort of thing is probably acceptable
if the program is short-lived, but even then you can run into
file-sharing conflicts. Note that current CPython versions are pretty
good about closing files if you don't save the object, but other
versions might not get around to it for a while.
>
>
> Mic
>
* (you can partially solve this if you put the open inside a list
comprehension or equivalent, but exceptions still rear their ugly heads)
--
DaveA
More information about the Tutor
mailing list