I would like to do a Python application that prints data to stdout, but
not the common way. I do not want the lines to be printed after each
other, but the old lines to be replaced with the new ones, like wget
does it for example (when downloading a file you can see the percentage
increasing on a same line).
I looked into the curses module, but this seems adapted only to do a
whole application, and the terminal history is not visible anymore when
the application starts.
Any ideas?
Thanks,
Remi
> I would like to do a Python application that prints data to stdout, but
> not the common way. I do not want the lines to be printed after each
> other, but the old lines to be replaced with the new ones, like wget
> does it for example (when downloading a file you can see the percentage
> increasing on a same line).
sys.stdout.write("Here's the first line")
time.sleep(1)
sys.stdout.write("\rAnd this line replaces it.")
--
Grant
--
R�mi
> Thank you for your answer, but that does not work:
Works fine for me.
> the second line is printed after the first one.
Not when I run it.
There's not much more I can say given the level of detail
you've provided.
--
Grant
If I understand well, \r erases the last line. How about erasing the
previous lines?
For example when writing
sys.stdout.write("1\n2\n")
sys.stdout.write("\r3")
the "1" is still visible.
--
Rémi
On 24 jan, 20:53, Grant Edwards <inva...@invalid.invalid> wrote:
That does not work on my system, because sys.stdout is line buffered.
This causes both strings to be written when sys.stdout is closed because
Python is shutting down.
This works better:
import sys, time
sys.stdout.write("Here's the first line")
sys.stdout.flush()
time.sleep(1)
sys.stdout.write("\rAnd this line replaces it.")
sys.stdout.flush()
Hope this helps,
-- HansM
That's correct of course.
> This causes both strings to be written when sys.stdout is closed because
> Python is shutting down.
>
> This works better:
>
> import sys, time
>
> sys.stdout.write("Here's the first line")
> sys.stdout.flush()
> time.sleep(1)
> sys.stdout.write("\rAnd this line replaces it.")
> sys.stdout.flush()
Or you can tell Python to do unbuffered output:
#!/usr/bin/python -u
--
Grant Edwards grante Yow! I'm using my X-RAY
at VISION to obtain a rare
visi.com glimpse of the INNER
WORKINGS of this POTATO!!
You might want to take a look at the readline module.
~Sean
Thanks everyone for your answers, that helped a lot.