简体   繁体   中英

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)

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM