Python code to replace shell scripts

Alan Gauld alan.gauld at btinternet.com
Fri Dec 12 10:02:49 CET 2003


On 12 Dec 2003 01:58:23 GMT, William Park <opengeometry at yahoo.ca>
wrote:

> > count=`ps -ef|grep "pattern"|wc -l`
> > for count in `echo $count`
> >   do
> >   done
> > fi

In this case you could change the style to a more slightly 
more pythonic approach:

print len(os.popen('ps -ef|grep "pattern"').read().split())

> In any case, although Python does something well, shell does most things
> better.  (It's okey... I've got my helmet on.)

Shell is generally better at quickly gluing together existing
commands. But does so at a significant cost in machine resources
and often execution time. Python provides a different approach
that is generally better where the solution must be repeated
often or where no suitable set of commands already exists.

Alan G.
Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Python-list mailing list