0

How would you make print only print one line each time like

for i in range(1,100)
    print i

but then i would want it to print one line each time so it would be: 1 and then 1 would be erased then it would print 2 and so on.

P'sao
  • 2,946
  • 11
  • 39
  • 48
  • 1
    Related: [in-place-progress-output-in-the-terminal-or-console](http://stackoverflow.com/questions/2336474/in-place-progress-output-in-the-terminal-or-console/) –  Apr 29 '11 at 01:46

6 Answers6

3

A trailing comma suppresses the newline, a carriage return moves to the beginning of the line.

for i in range(1,100):
    print "\r",i,

This works on Linux.

Keith
  • 42,110
  • 11
  • 57
  • 76
  • It also works for me on Windows (in the interactive Python 2.6 shell, anyway). – John Y Apr 29 '11 at 01:56
  • It doesn't work for me (Ubuntu 10.10) as the output is not flushed to the screen (this generally only happens when a newline is printed, execution finished or it is explicitly flushed). You need to use ``sys.stdout.flush()`` to make sure the output appears, as in [tMC's answer](http://stackoverflow.com/questions/5827000/print-that-only-prints-one-line/5827046#5827046) – Blair Apr 29 '11 at 02:58
3

like this?

import sys
import time
>>> for i in range(1, 100):
...     sys.stdout.write(str(i))
...     sys.stdout.flush()
...     time.sleep(0.2)
...     sys.stdout.write("\b"*4)
<numbers count here>
>>>
tMC
  • 18,105
  • 14
  • 62
  • 98
  • Oh, idk if this would work on windows, and it is cleaner to use "\r" over printing backspaces. On linux if you don't flush the buffer, you don't see anything until the loops ends. – tMC Apr 29 '11 at 01:53
  • Argh, I was just about to post that. I was going to use `chr(8)` though -- I forgot that `\b` does the same thing. – senderle Apr 29 '11 at 01:53
1

you can try this :

import sys
import time
for num in range(100):
    sys.stdout.write("\r "+str(num))
    sys.stdout.flush()
    time.sleep(0.3)
Yuda Prawira
  • 12,075
  • 10
  • 46
  • 54
0

Depends on your platform. If the curses module is available to you, use that. If you're on Windows, and using a sufficiently old version of Python (2.6 and earlier), you could try Fredrik Lundh's Console.

John Y
  • 14,123
  • 2
  • 48
  • 72
0

It looks to me like you are looking for a progress bar. If so check out some ready-made ones.

Even if you are not making a progress bar, the code therein would need to do something similar. Perhaps it can give you some clues.

As I am not a python guy, I don't know the python syntax. For Perl you can add a \r character to an interpolated quotation, this moves the cursor back to the beginning of the line, thus overwriting any previous text on the next iteration. Perhaps python has a similar character.

perl -e 'print "$_\r" for (1..100); print "\n";'
Joel Berger
  • 20,180
  • 5
  • 49
  • 104
0

Simplest way is to use the backspace character \b like

from __future__ import print_function
import sys, time

for i in range(1, 100):
  print('\b\b\b%d' % i, end='')
  sys.stdout.flush()
  time.sleep(0.1)
Reci
  • 4,099
  • 3
  • 37
  • 42