Embedding Python in Windows Applications

Mark Hammond MarkH at ActiveState.com
Tue Jan 2 09:38:47 EST 2001


Gerson Kurz wrote:

> Thanks, but how would you do that ? For starters, I have tried the
> following test code (in your excellent PythonWin):
> 
> -----------------------------------------------
> import sys
> 
> class myclass:
>     def __init__(self):
>         self.file = open("D:\\test.log","w")
>     
>     def write(self,str):       
>         self.file.write(str)
> 
>     def __del__(self):
>         self.file.close()
> 
> old_stdout = sys.stdout
> sys.stdout = myclass()
> print "Hello, world"
> sys.stdout = old_stdout
> print "Done."
> -----------------------------------------------
> 
> Now, as expected the first print statement will write to the file,
> BUT, the second statement will raise an IO Error.

That is exactly what we expect - this discussion started because your 
"old_stdout" was invalid in the first place.  As you demonstrate, you 
can override it, but in your specific example, there is no need to 
restore it.

Mark.




More information about the Python-list mailing list