"Try:" which only encompasses head of compound statement

Lawrence D'Oliveiro ldo at geek-central.gen.new_zealand
Tue Aug 28 00:57:26 CEST 2007


In message <1188250535.607963.42540 at 22g2000hsm.googlegroups.com>, 
Jameson.Quinn at gmail.com wrote:

> try:
>     for line in open(myFileName):
>         count += 1
> except IOError:
>     print "Can't open myfile"
> 
> (I know, this is bad, I never close the file, but its just for
> illustration).

No, that's fine. The file object returned by "open" is refcounted, so when
the last reference to it disappears, it is closed.



More information about the Python-list mailing list