views:

50

answers:

1

Hi,

I want to run the 'time' unix command from a Python script, to time the execution of a non Python app. I would use the os.system method. Is there any way to save the output of this in Python? My goal is to run the app several times, save their execution times and then do some statistics on them.

Thank You

+3  A: 

You really should be using the subprocess module to run external commands. The Popen() method lets you specify a file object where stdout should go (note, you can use any Python object that behaves like a file, you don't necessarily need to write it to a file).

For instance:

import subprocess
log_file = open('/path/to/file', 'a')

return_code = subprocess.Popen(['/usr/bin/foo', 'arg1', 'arg2'], stdout=log_file).wait()
Josh Wright