0

When using wget in a Linux terminal, the last line printed in the terminal is being overwritten as the download progresses, to reflect the progress.

Can I overwrite the last line of the terminal in Python? (targeting Linux only)

Juicy
  • 10,990
  • 33
  • 107
  • 196
  • http://stackoverflow.com/questions/3160699/python-progress-bar/3162864 Has been answered here – ssm Oct 16 '14 at 00:48
  • related: [Text Progress Bar in the Console](http://stackoverflow.com/q/3173320/4279). – jfs Oct 16 '14 at 00:59

2 Answers2

4

You can use escape sequences.

You might be familiar with "\n" for new line. There's also "\b" for backspace and "\r" for carriage return.

import time
for i in range(10):
    sys.stdout.write("\r{}".format(i))
    sys.stdout.flush()
    time.sleep(1)
Fedalto
  • 1,397
  • 9
  • 13
1

You could use blessings module to write on the last line in a terminal on Linux:

from blessings import Terminal # $ pip install blessings

t = Terminal()
with t.location(0, t.height - 1):
    print('This is at the bottom.')
jfs
  • 374,366
  • 172
  • 933
  • 1,594