HOWTO execute an external program?

Emile van Sebille emile at fenx.com
Thu Nov 8 11:48:54 EST 2001


"Patrick Guio" <patricg at fys.uio.no> wrote in message
news:Pine.LNX.4.30.0111081248470.5502-100000 at fyspc-rp18.uio.no...
> I want to run my simulation code in a loop with different argument values,
> I now use a bourne shell script like
>
> timelogger="/usr/bin/time -p
> execname="picsim"
> for B in 0.1 0.2 0.3 0.4 0.5 0.6 ; do
>   if [ ! -f $dir/${filename}B$B.hdf ] ; then
>     command="$execname bvector=0,$B -o $filename.hdf -i $conffile"
>     ($timelogger $command > run.log) > run.cmd 2>&1
>     ....
>   fi
> done
>
> Now I need to calculate, at each iteration, new values of several other
> arguments and it looks like python is nice to do maths operations.
> So what I need is
>
> iteration loop
>   calculate argument values val1,val2,...valn
>   command="myprog -arg1="+val1+ " -arg2="+val2....
>   execute the command AND time it AND log the stdout and stderr of running
> it
> end
>

Something building on this could do it:

import os

for command, filetype in [('locate','.py'), ('locate','.pl'),
('locatex','.html')]:
    cmd = "(time %s %s >>test.log) "
    i, o, e = os.popen3(cmd % (command, filetype))
    timings = e.readlines()
    print '%s : %s' % (filetype, timings)

which yields:

.py : ['\n', 'real\t0m6.589s\n', 'user\t0m1.900s\n', 'sys\t0m3.810s\n']
.pl : ['\n', 'real\t0m1.932s\n', 'user\t0m1.390s\n', 'sys\t0m0.070s\n']
.html : ['/bin/sh: locatex: command not found\n', '\n', 'real\t0m0.003s\n',
'user\t0m0.000s\n', 'sys\t0m0.000s\n']

HTH,

--

Emile van Sebille
emile at fenx.com

---------




More information about the Python-list mailing list