>> print("123#\b45...">

What can I use to jump a single line to a terminal in Python?

I can use one character with \b :

 >>> print("123#456") 123#456 >>> print("123#\b456") 123456 

But this does not work if line break is involved:

 >>> print("123#\n456") 123# 456 >>> print("123#\n\b456") 123# 456 

Is there any way to cancel line break?

I ask about this because I have progress on the previous line:

53%

And I use \b to update the value. But if someone prints something, it breaks him. I tried to create a line buffer and print enough "\ b" to compensate for it, and then print the buffer back. But this does not work if there are line breaks.

+7
python line-breaks
source share
1 answer

One possible (slightly hacky) solution is to use '\ 033 [1A' to return a single line. Replace 1 with the number of lines to go back. There are several other escape sequences that you can use to control the cursor. Check out the full list at: http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html

 - Position the Cursor: \033[<L>;<C>H Or \033[<L>;<C>f puts the cursor at line L and column C. - Move the cursor up N lines: \033[<N>A - Move the cursor down N lines: \033[<N>B - Move the cursor forward N columns: \033[<N>C - Move the cursor backward N columns: \033[<N>D - Clear the screen, move to (0,0): \033[2J - Erase to end of line: \033[K - Save cursor position: \033[s - Restore cursor position: \033[u 

Please note that this probably will not work for all terminals.

+9
source share

All Articles