script files with python (instead of tcsh/bash)?

Esmail ebonak at hotmail.com
Mon Mar 23 10:04:46 EDT 2009


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.


> I presume you mean something like this
> 
>    ... | awk '{print $2}'
> 
> In python, assuming you've got the line in the "line" variable, then
> 
> In python an equivalent of the above would be
> 
> import fileinput
> for line in fileinput.input():
>     print line.split()[1]


cool .. that is what I came up with too!

> Note that the fileinput module is really useful for making shell
> command replacements!

yes, I am going to have to process a number of files, so I'm also
looking at glob and os.walk(??)

>>  The other things I need to do consist of moving files, manipulating file
>>  names and piping outputs of one command to the next, so I'm digging into
>>  the documentation as much as I can.
> 
> Read up on the os module and the subprocess module.  You'll find you
> need to do much less piping with python as with shell because it has
> almost everything you'll need built in.
> 
> Using built in functions is much quicker than fork()-ing an external
> command too.

Thanks again for all the useful info and examples, this really has been
a great help.

Esmail




More information about the Python-list mailing list