Juicy
Juicy

Reputation: 12530

Print over last line in terminal

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)

Upvotes: 0

Views: 1771

Answers (2)

Fedalto
Fedalto

Reputation: 1567

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)

Upvotes: 4

jfs
jfs

Reputation: 414645

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.')

Upvotes: 1

Related Questions