with open('com1', 'r') as f:
tjreedy at udel.edu
Sun Apr 5 08:48:20 CEST 2009
Lawrence D'Oliveiro wrote:
> In message <91e09eaf-5a25-4a6b-b131-
> a5245970b337 at f19g2000yqh.googlegroups.com>, gert wrote:
>> On Apr 4, 12:58 am, Lawrence D'Oliveiro <l... at geek-
>> central.gen.new_zealand> wrote:
>>> In message <8bc55c05-19da-41c4-
>>> b916-48e0a4be4... at p11g2000yqe.googlegroups.com>, gert wrote:
>>>> with open('com1', 'r') as f:
>>>> for line in f:
>>> Why bother, why not just
>>> for line in open('com1', 'r') :
>>> print line
>> So its does the same thing as with right ?
> Why do you need a with?
>> Automatic closing and finalizing stuff.
> All Python objects are reference-counted.
Nope. Only in CPython, and even that could change.
> Once the file object becomes
> inaccessible, it is automatically closed. Simple.
Even in CPython, that would not be true now is the object became
involved in or became a dependent of a reference cycle.
> Note: I wouldn't do this for files open for writing. I'd prefer to make sure
> those were properly flushed and closed, if only to catch any I/O errors.
More information about the Python-list