Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Terminal application with non-standard print

0 views
Skip to first unread message

Rémi

unread,
Jan 24, 2010, 2:27:44 PM1/24/10
to
Hello everyone,

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

Grant Edwards

unread,
Jan 24, 2010, 2:33:16 PM1/24/10
to
On 2010-01-24, R?mi <babe...@yahoo.fr> wrote:

> 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

unread,
Jan 24, 2010, 2:39:53 PM1/24/10
to
Thank you for your answer, but that does not work : the second line is
printed after the first one.

--
R�mi

Grant Edwards

unread,
Jan 24, 2010, 2:53:32 PM1/24/10
to
On 2010-01-24, R?mi <babe...@yahoo.fr> wrote:

> 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


Message has been deleted

Rémi

unread,
Jan 24, 2010, 3:35:09 PM1/24/10
to
My apologies, I did not run the lines properly.
Thanks, that works great now.

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:

Hans Mulder

unread,
Jan 25, 2010, 3:55:03 PM1/25/10
to

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

Message has been deleted

Grant Edwards

unread,
Jan 25, 2010, 5:20:54 PM1/25/10
to
On 2010-01-25, Hans Mulder <han...@xs4all.nl> wrote:
> Grant Edwards wrote:
>> On 2010-01-24, R?mi <babe...@yahoo.fr> wrote:
>>
>>> 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.")
>
> That does not work on my system, because sys.stdout is line buffered.

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!!

Sean DiZazzo

unread,
Jan 25, 2010, 5:30:58 PM1/25/10
to

You might want to take a look at the readline module.

~Sean

Rémi

unread,
Jan 27, 2010, 6:23:34 AM1/27/10
to

Thanks everyone for your answers, that helped a lot.

0 new messages