E
Esmail
Hello David,
Cool .. stored away for future reference.
In this case I believe I needed the contents in a list because the line
I was looking for was above one that I could easily identify. Ie, once
I had the index, I could go back/up one index to find the line I needed
to process.
thanks for reminding me about the 'with' clause. I agree with closing
files and in general being mindful about allocated resources.
Cheers,
Esmail
R. David Murray said:Here's a more Pythonic way to do that:
with open('somefile') as f:
for line in f:
if 'somestring' in line:
#do something
In other words, you don't have to read the lines into a list first if all
you are going to do is iterate through them.
Cool .. stored away for future reference.
In this case I believe I needed the contents in a list because the line
I was looking for was above one that I could easily identify. Ie, once
I had the index, I could go back/up one index to find the line I needed
to process.
(The 'with' clause closes
the file at block exit...which is overkill if this is all the program
is doing since the file will be closed at program termination anyway,
but is a good habit to get in to.)
thanks for reminding me about the 'with' clause. I agree with closing
files and in general being mindful about allocated resources.
Cheers,
Esmail