question for sys.stdout

Pierre Barbier de Reuille pierre.barbier at
Thu Nov 4 14:31:01 CET 2004

MMmhh ... I'm not sure what you _really_ want to do ???

You want to replace stdout by a custom file-like object ? Because I 
don't see what you call "doing rewrite" ... You should give a clear 
example of the behaviour you want.


Leon a écrit :
> sorry....
> how to replace sys.stdout to file-like object ?
> I use os module fdopen() and file object fileno()
> stdout = os.fdopen(sys.stdout.fileno(),"w")
> or
> stdout = os.fdopen(os.dup(sys.stdout.fileno()),"w")
> it still can't do rewrite, it result is append content
> .>_<.    Please help me
> "Pierre Barbier de Reuille" <pierre.barbier at> 
> ???????:4189ec52$0$6282$626a14ce at
>>If you want to "rewrite" a "stdout" object, just write a file-like object 
>>and replace sys.stdout !
>>Then don't care about the old stdout anymore ... it's a pipe and it won't 
>>bother you anymore :)
>>Another way to do so is the "C" way ... ie. using os.dup2. The advantage 
>>of the "dup2" method is the replacement of the stdout of the whole program 
>>(so, if you use external functions writing in stdout it will work), but 
>>the drawback is you need to have a real file (ie. file, pipe, ...)
>>Depends on what you want to use :)
>>Leon a écrit :
>>>"stdout" is file object, it open mode is "w"
>>>and it have a close() fuction.....
>>>while it run close(), how to reopen it
>>>because I want to do rewrite it
>>>stdout content update

More information about the Python-list mailing list