Python で、ターミナルに文字を出力してから消す

投稿者: ytyng 1年, 3ヶ月 前

シーケンス \b のバックスペースで、プリントした文字を消すことができる。

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

heavy_method(...)...

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

コンテキストマネージャーで書くと

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(...)
現在未評価

コメント

アーカイブ

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