sys.stdout .>_<.

Mike Meyer mwm at
Fri Nov 5 23:42:06 CET 2004

[Format repaired]

Pierre Barbier de Reuille <pierre.barbier at> writes:
> Leon a écrit :
>> import os,sys
>> a = os.fdopen(sys.stdout.fileno(),"w")
>> b = os.fdopen(sys.stdout.fileno(),"w")
>> a.write("test1 ")
>> b.write("test2 ")
>> stdout result is
>> test1 test2
>> but use general file object such as...
>> a = open("test.txt","w")
>> b = open("test.txt","w")
>> a.write("test1")
>> b.write("test2")
>> the "test.txt" file content is
>> test2
>> how to do this result to sys.stdout
> It's not possible !


> stdout is usually a pipe ... and you cannot go back in a pipe ...
> Try with any pipe you should have the same result.

Actually, his second example didn't "go back". The two file pointers
started at the beginning of the file. The first write writes "test1"
into the file starting at the beginning. The second write writes
"test2" at the file starting at the beginning, overwriting the results
of the first write.

> The only way toi achieve this is to assume an ANSI terminal and to use
> ANSI codes to go back ("\r" to go at the beginning of the line "^h" to
> go back one char)

The other altnerative is the curses module.

Mike Meyer <mwm at>
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

More information about the Python-list mailing list