Skip to content

Instantly share code, notes, and snippets.

@FlxVctr
Created July 7, 2021 11:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FlxVctr/95abee5c5d584064673bbe887c711873 to your computer and use it in GitHub Desktop.
Save FlxVctr/95abee5c5d584064673bbe887c711873 to your computer and use it in GitHub Desktop.
A function to overwrite the last line in the terminal/stdout
'''
Usage:
previous_text_length = overwrite('text to be written', 0)
previous_text_length = overwrite('next text to be written', previous_text_length)
'''
def overwrite(text, previous):
print('\b' * previous, ' ' * previous, end="\r")
print(text, end="")
sys.stdout.flush()
return len(text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment