question for sys.stdout
square690410 at yahoo.com.tw
Thu Nov 4 11:20:53 CET 2004
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")
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 cirad.fr>
???????:4189ec52$0$6282$626a14ce at news.free.fr...
> 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