How do I make the cursor in the terminal go up using python so I can overwrite text printed e.g. 3 lines ago using \r?
I've tried to use: print("e.g.", end="\r")
But that overwrites only the latest line that was printed.
CodePudding user response:
You can make the cursor move vertically up by using \033[nA
where n
is the amount of lines you need to go up. This only works if your terminal(or emulator) supports ANSI escape sequences.
You can test this by,
echo -n -e '\033[2A'
Sample python code to visualize this
from time import sleep
print("asdfasdf")
sleep(1)
print("asdfasdf")
sleep(1)
print("asdfasdf")
sleep(1)
print('\033[3A')
print("hello wolrd")
CodePudding user response:
\r - is the beginning of the current line.
\033[F - ASCII command to move the cursor one line up.
Try this one, to understand the logic
print('line1\nline2\nline3\033[F*')