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

Python
2023-01-03 02:21 (3 years ago)
Cursor’s Wink
この記事をテーマにした曲を再生

シーケンス \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(...)

現在の評価: 5.0 (1)
著者は、アプリケーション開発会社 Cyberneura を運営しています。
開発相談をお待ちしています。

アーカイブ