slicing the end of a string in a list

P

Paul Rubin

Steve Holden said:
[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?

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.

I wonder whether PEP 343 should be extended somehow. Hmm.

[line.rstrip() for line within open('C:\\switches.txt')] # ugh!!

Other ideas?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top