question for sys.stdout

L

Leon

"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
 
P

Pierre Barbier de Reuille

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 :
 
L

Leon

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
 
D

Diez B. Roggisch

how to replace sys.stdout to file-like object ?

sys.stdout = open("/this/is/so/simple", "w")
 
P

Pierre Barbier de Reuille

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.

Pierre

Leon a écrit :
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,210
Messages
2,571,091
Members
47,691
Latest member
Jenny-jane

Latest Threads

Top