Output and Clear Text in Terminal with Python

Python
2023-01-03 11:21 (2 years ago) ytyng

With the backspace sequence \b, you can erase printed characters.

print('requesting...', end='', flush=True)

heavy_method(...)...

print('\b' * 13, end='', flush=True)

When written with a context manager:

import contextlib


@contextlib.contextmanager
def print_and_erase(text):
    print(text, end='', flush=True)
    yield
    print('\b' * len(text), end='', flush=True)


with print_and_erase('requesting...'):
    heavy_method(...)
Currently unrated
The author runs the application development company Cyberneura.
We look forward to discussing your development needs.

Archive

2025
2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011