redirecting output from spawned processes

Jef Mangelschots jef.mangelschots at iname.com
Fri Nov 15 22:04:17 EST 2002


I know about os.system() and have done that.

I use this to run some long duration processing command-line tools
which I run overnight. I used os.system() for this, for wich I
redirected the output to a log-file.

But I noticed repeatedly in the morning that my script misteriously
stopped in the middle of processing.

I had the impression that running these tools from os.system()
somehow messes up things.

I was hoping that spawn() would keep my running process intact, even
if the called processes fail somehow.

But then it was not obvious to me how to redirect output from my
process to a log-file.




On Fri, 15 Nov 2002 12:28:09 -0800, Trent Mick
<trentm at ActiveState.com> wrote:

>[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