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

how to suppress carriage return/line feed?

104 views
Skip to first unread message

TuxTrax

unread,
Oct 30, 2002, 2:20:27 AM10/30/02
to
Hi all.

I'm sure this is simple, but I am having trouble here. I have a repetitive
task in a program, and in verbose mode I want to simply print progress like
so:

for index in range(bookmark, len(poster)):
if verbose: print "Creating database entry %s out of %s" % \
(counter, number_of_messages)

Simple enough. But the behavior I am looking for is for the line to print
thusly:

"Creating database entry 1 out of 50"

And then do a carriage return *without* the linefeed, so that the
cursor goes back to the beginning of the screen line just displayed,
before displaying:

"Creating database entry 2 out of 50"

and so on. In other words, I need to suppress the normal behavior of the
print statement so that the monitor is displaying one line that continues to
be updated, rather than a screen full of scrolling lines. There has got to
be a simple way of doing this, without having to fool with curses. The '\r'
escape sequence dosen't do it.

Thanks all,

Cheers,

Mathew

--
TuxTrax (n.) An invincible, all terrain, Linux driven armored assault
vehicle that can take as much fire as it gives ;-)

Yes, I am a Penguin cult high priest. Flipper readings upon request.

ROT13 this email address to mail me:
bar jbeq abg guerr - uvtu qrfreg zna, ng lnubb qbg pbz

Erik Max Francis

unread,
Oct 30, 2002, 3:11:53 AM10/30/02
to
TuxTrax wrote:

> for index in range(bookmark, len(poster)):
> if verbose: print "Creating database entry %s out of %s" % \
> (counter, number_of_messages)
>
> Simple enough. But the behavior I am looking for is for the line to
> print
> thusly:
>
> "Creating database entry 1 out of 50"
>
> And then do a carriage return *without* the linefeed, so that the
> cursor goes back to the beginning of the screen line just displayed,
> before displaying:
>
> "Creating database entry 2 out of 50"

If you want finer control, try using sys.stdout.write directly,
presumably with a flush:

sys.stdout.write("Creating database entry ...\r")
sys.stdout.flush()

Note that on some conceivable terminals this still won't have the
desired effect, and you might have to resort to using curses or
something similar.

--
Erik Max Francis / m...@alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/ \ Life is not a spectacle or a feast; it is a predicament.
\__/ George Santayana
Church / http://www.alcyone.com/pyos/church/
A lambda calculus explorer in Python.

Bengt Richter

unread,
Oct 30, 2002, 3:28:21 AM10/30/02
to
On Wed, 30 Oct 2002 07:20:27 -0000, Tux...@fortress.tuxnet.net (TuxTrax) wrote:

>Hi all.
>
>I'm sure this is simple, but I am having trouble here. I have a repetitive
>task in a program, and in verbose mode I want to simply print progress like
>so:
>
>for index in range(bookmark, len(poster)):
> if verbose: print "Creating database entry %s out of %s" % \
> (counter, number_of_messages)
>

if verbose: print "\rCreating database entry %s out of %s " % (counter, number_of_messages),
^^-[1] ^^^^-[2] [3]-^
[1]: The carriage return
[2]: Enough spaces so if your printable part can get shorter, you still erase previous print
[3]: Make print not output a \n (but will put space in front of next output -- which if
bothersome, you can eliminate two ways, most straight forward is below.

>Simple enough. But the behavior I am looking for is for the line to print
>thusly:
>
>"Creating database entry 1 out of 50"
>
>And then do a carriage return *without* the linefeed, so that the
>cursor goes back to the beginning of the screen line just displayed,
>before displaying:
>
>"Creating database entry 2 out of 50"
>
>and so on. In other words, I need to suppress the normal behavior of the
>print statement so that the monitor is displaying one line that continues to
>be updated, rather than a screen full of scrolling lines. There has got to
>be a simple way of doing this, without having to fool with curses. The '\r'
>escape sequence dosen't do it.
>

>>> if 1: # just to let me type the following in one block ;-)
... import sys
... for i in xrange(10000):
... sys.stdout.write('\rNo space to the left of this. %10s' %i)
... print '\n<<--[Use \\r or \\n here for effect desired].'
...
No space to the left of this. 9999
<<--[Use \r or \n here for effect desired].

Regards,
Bengt Richter

TuxTrax

unread,
Oct 30, 2002, 6:32:22 AM10/30/02
to
On 30 Oct 2002 08:28:21 GMT, Bengt Richter Wrote in
Steve Ballmers hair grease:

Thanks Bengt, but \r dosen't produce the desired effect. It still causes
each consecutive printed line to scroll up even after the screen is full.

don't know about sys.stdout.write. I haven't used that method before.

Terry Reedy

unread,
Oct 30, 2002, 9:27:08 AM10/30/02
to

"TuxTrax" <Tux...@fortress.tuxnet.net> wrote in message
news:slrnarv28n....@fortress.tuxnet...

> Simple enough. But the behavior I am looking for is for the line to
print
> thusly:
>
> "Creating database entry 1 out of 50"
>
> And then do a carriage return *without* the linefeed, so that the
> cursor goes back to the beginning of the screen line just displayed,
> before displaying:
>
> "Creating database entry 2 out of 50"

On Win98 console, add \r and suppress \r\n with trailing ',':

>>> for i in range(10): print "line %d\r" % i, # note last char
...
>>> 9

IE, 0 to 9 printed in same space instead of on 10 lines - and ">>> '
over 'line'.

Terry J. Reedy

IE


Ned Batchelder

unread,
Oct 30, 2002, 9:50:18 AM10/30/02
to
A trailing comma on the print statement will suppress the newline.
This works for me on Windows:

import time

for i in range(10):
print "Counting %s\r" % i,
time.sleep(1)

--Ned.
http://www.nedbatchelder.com

Tux...@fortress.tuxnet.net (TuxTrax) wrote in message news:<slrnarv28n....@fortress.tuxnet>...

Bengt Richter

unread,
Oct 30, 2002, 11:46:16 AM10/30/02
to

Did you add the trailing comma? The line above that mentions "desired effect"
is only to preserve or not preserve the last of the non-scrolling lines that
would precede it, when it all finishes.

>
>don't know about sys.stdout.write. I haven't used that method before.
>

Well, Erik mentioned some systems do make new lines in spite of the above
(though stdout ought to work, adding flush if necessary).

I'm curious which of those you have.
It works for me on NT4 and slackware Linux 2.2.16

Regards,
Bengt Richter

Erik Max Francis

unread,
Oct 30, 2002, 2:56:44 PM10/30/02
to
Bengt Richter wrote:

> Did you add the trailing comma? The line above that mentions "desired
> effect"
> is only to preserve or not preserve the last of the non-scrolling
> lines that
> would precede it, when it all finishes.

Using print, even with the trailing comma, is almost never a good idea
when you're trying to do precision printing. Using sys.stdout.write
directly is almost always preferable in those cases.

In particular, using print ..., will result in spurious spaces being
printed, which will be especially troublesome when you're trying to
overwrite the same line.

--
Erik Max Francis / m...@alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE

/ \ When one is in love, a cliff becomes a meadow.
\__/ (an Ethiopian saying)
Python chess module / http://www.alcyone.com/pyos/chess/
A chess game adjudicator in Python.

Erik Max Francis

unread,
Oct 30, 2002, 2:58:21 PM10/30/02
to
Ned Batchelder wrote:

> A trailing comma on the print statement will suppress the newline.
> This works for me on Windows:
>
> import time
>
> for i in range(10):
> print "Counting %s\r" % i,
> time.sleep(1)

You're going to want to put a sys.stdout.flush() after printing anyway,
since in general you have no way of knowing whether stdout is
line-buffered (and it can very). Since you have to interact with
sys.stdout directly, you might as well just go ahead and use
sys.stdout.write directly as well.

Bengt Richter

unread,
Oct 30, 2002, 3:28:57 PM10/30/02
to
On Wed, 30 Oct 2002 11:56:44 -0800, Erik Max Francis <m...@alcyone.com> wrote:

>Bengt Richter wrote:
>
>> Did you add the trailing comma? The line above that mentions "desired
>> effect"
>> is only to preserve or not preserve the last of the non-scrolling
>> lines that
>> would precede it, when it all finishes.
>
>Using print, even with the trailing comma, is almost never a good idea
>when you're trying to do precision printing. Using sys.stdout.write
>directly is almost always preferable in those cases.
>
>In particular, using print ..., will result in spurious spaces being
>printed, which will be especially troublesome when you're trying to
>overwrite the same line.

"Spurious" sounds buggy. They really are predictable. I was going to mention
softspace as the other way to control them, but since you have to go to
sys.stdout to do that anyway, you might as well use sys.stdout.write.

I think more troublesome than the print spaces will be if you don't use a
format that generates a fixed length output, whether you print it or pass it
to sys.stdout.write. In that case, if you move from a part of the program
that outputs a long message line to a part that outputs a short one,
you'll have (predictable ;-) spurious leftover trailing characters.

Your reminder to flush is a good idea ;-)

Regards,
Bengt Richter

Erik Max Francis

unread,
Oct 30, 2002, 4:11:49 PM10/30/02
to
Bengt Richter wrote:
>
> On Wed, 30 Oct 2002 11:56:44 -0800, Erik Max Francis <m...@alcyone.com> wrote:
>
> >Bengt Richter wrote:

> "Spurious" sounds buggy. They really are predictable.

Spurious wasn't quite the right word; they of course do not happen
randomly. They are, however, likely to be surprising to an unsuspecting
user thinking that print ..., is an appropriate way to get precise
formatting.

--
Erik Max Francis / m...@alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE

/ \ It's a man's world, and you men can have it.
\__/ Katherine Anne Porter
Rules for Buh / http://www.alcyone.com/max/projects/cards/buh.html
The official rules to the betting card game, Buh.

TuxTrax

unread,
Oct 30, 2002, 4:19:06 PM10/30/02
to
On Wed, 30 Oct 2002 11:58:21 -0800, Erik Max Francis Wrote in
Steve Ballmers hair grease:

> Ned Batchelder wrote:


>
>> A trailing comma on the print statement will suppress the newline.
>> This works for me on Windows:
>>
>> import time
>>
>> for i in range(10):
>> print "Counting %s\r" % i,
>> time.sleep(1)
>
> You're going to want to put a sys.stdout.flush() after printing anyway,
> since in general you have no way of knowing whether stdout is
> line-buffered (and it can very). Since you have to interact with
> sys.stdout directly, you might as well just go ahead and use
> sys.stdout.write directly as well.
>

Erik, if you would, I would like to see an example of sys.stdout.write()
and flush() in action, with a sample codelet. Thanks,

Erik Max Francis

unread,
Oct 30, 2002, 5:01:57 PM10/30/02
to
TuxTrax wrote:

> Erik, if you would, I would like to see an example of
> sys.stdout.write()
> and flush() in action, with a sample codelet. Thanks,

import sys, time

for i in range(10):
sys.stdout.write("Counting %s\r")
sys.stdout.flush()
time.sleep(1)

As I pointed out earlier, this still isn't a perfect solution; you're
not guaranteed that some terminals won't interpret '\r' (CR)
differently. For instance, on a Mac, this would likely print each one
on a separate line. On Windows and UNIX, it will likely work, but again
you could conceivably be using some terminal that does something
different.

If you really want total positioning control, you'll have to go with
some solution like curses (but that isn't available for Windows).

--
Erik Max Francis / m...@alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE

0 new messages