an ugly file-reading pattern
Avner Ben
avner at skilldesign.com
Mon Apr 14 12:53:32 EDT 2003
"Robin Munn" <rmunn at pobox.com> wrote in message
news:slrnb9ksaa.1q1.rmunn at localhost.localdomain...
> I must have been dealing with too much spam recently: when I saw "I have
> tried this and it works," I thought the next thing I was going to see
> would be instructions for sending $5 to these five addresses... :-)
Good thing they don't put filters on messages sent to newsgroups...
> You're right, I didn't close the file explicitly. But explicit is better
> than implicit, so IMHO it's better to give a name to the file object so
> that you can close it explicitly:
>
> input_file = file("somefile")
> for line in input_file:
> process(line)
> input_file.close()
The C# language has a nice construct: using (variable = whatever) { /*
code */ }. The variable in the using header is guaranteed to be
garbage-collected at the end of the block (or so I think). Could be useful
here.
Avner.
More information about the Python-list
mailing list