redirecting output from spawned processes

Trent Mick trentm at ActiveState.com
Fri Nov 15 15:28:09 EST 2002


[Jef Mangelschots wrote]
> How do I redirect the output of 'someapp' to the file log_file ?
> 
> 
>            log_file = 'c:\output.log'
> 
>            os.spawnv(os.P_WAIT, 'some_app', 'p1', 'p2')

A couple of ways:

    os.system("someapp > %s" % log_file)

or:

    # get the output
    fout = os.popen(someapp)
    output = fout.read()
    fout.close()

    # then write it to your file
    flog = open(log_file, 'w')
    flog.write(output)
    flog.close()

I think you want the former though. To redirect stderr as well you need
to do a little bit more work:

    os.system("someapp > %s 2>&1" % log_file)

or:

    ... look at os.popen3()


Cheers,
Trent

-- 
Trent Mick
TrentM at ActiveState.com




More information about the Python-list mailing list