No need to close file?

Grant Edwards grante at visi.com
Tue Jul 18 17:10:11 EDT 2006


On 2006-07-18, T <ty.2006 at yahoo.com> wrote:

>>> for line in file('foo', 'r'):
>>>   print line

>> Good programming practice says that if you open it - you close it.
>>
>> And stay out of trouble ;-)

> How do I close the file in the above case?

Aye, there's the rub.

You can't close an anonymous file, so you have to give it a name.

   f = file('foo', 'r')
   for line in f:
      print line
   f.close()

-- 
Grant Edwards                   grante             Yow!  The PILLSBURY
                                  at               DOUGHBOY is CRYING for
                               visi.com            an END to BURT REYNOLDS
                                                   movies!!



More information about the Python-list mailing list