script files with python (instead of tcsh/bash)?
R. David Murray
rdmurray at bitdance.com
Tue Mar 24 02:02:57 CET 2009
Esmail <ebonak at hotmail.com> wrote:
> Hello again Nick,
> thanks for the additional script example. I was able to put
> something together where I read the whole file into a list
> as a series of lines (via readlines()) and then loop through
> the lines seeing if the target string was "in" the line .. seems
> to have worked reasonably well.
> I am sure over time I will pick up the more Python(ic?) ways of
> doing things.
Here's a more Pythonic way to do that:
with open('somefile') as f:
for line in f:
if 'somestring' in line:
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. (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.)
R. David Murray http://www.bitdance.com
More information about the Python-list