slicing the end of a string in a list

Steve Holden steve at holdenweb.com
Mon Mar 6 11:11:21 EST 2006


John Salerno wrote:
> Paul Rubin wrote:
> 
>>John Salerno <johnjsal at NOSPAMgmail.com> writes:
>>
>>>Interesting. So I would say:
>>>
>>>[line.rstrip() for line in open('C:\\switches.txt')]
> 
> 
> 
> How would I manually close a file that's been opened this way? Or is it 
> not possible in this case? Is it necessary?

It's not possible to perform an explicit close if, as in this case, you 
don't have an explicit reference to the file object.

In CPython it's not strictly necessary to close the file, but other 
implementations don't guarantee that a file will be closed after the 
last reference is deleted.

So for fullest portability it's better explicitly close the file.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd                 www.holdenweb.com
Love me, love my blog         holdenweb.blogspot.com




More information about the Python-list mailing list