[python-win32] Capturing stdout when running a Python Script

David Nicolson davidnicolson1 at hotmail.com
Fri Oct 27 10:43:12 CEST 2006


This might do what you want to achieve. It seems to make py2exe crash  
on exit, some file locking error. Works fine otherwise though.

#!/usr/bin/env python

import sys

class LogBuffer:
     def __init__(self):
         pass

     def flush(self):
         sys.__stdout__.flush()

     def write(self,string):
         string = string.strip()
         sys.__stdout__.write("Captured: "+string+"\n")

if __name__ == "__main__":
     logbuffer = LogBuffer()
     sys.stdout = logbuffer
     print "Hello"


On 27/10/2006, at 5:48 PM, Johan Lindvall wrote:

> Hi,
>
> Is there any way of capturing or displaying stdout when running a
> Python script? I know about:
>
> import sys
> sys.stdout = open("logfile.txt", "a")
>
> but I don't want to modify my scripts to achieve this.
>
> -- 
> /Johan.
> _______________________________________________
> Python-win32 mailing list
> Python-win32 at python.org
> http://mail.python.org/mailman/listinfo/python-win32





More information about the Python-win32 mailing list