Python Forum
what's wrong with "\033[F" and "\033[A" - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: what's wrong with "\033[F" and "\033[A" (/thread-13538.html)



what's wrong with "\033[F" and "\033[A" - kavindu - Oct-19-2018

I'm trying to write a script to clear and write over the last printed line by following a tutorial.
this is my code
import time
import sys
def delete_last_lines(n=1):
    for _ in range(n):
        sys.stdout.write(str(_))
        sys.stdout.write("\033[F") #back to previous line
        sys.stdout.write("\033[K") #clear line
        time.sleep(1)
delete_last_lines(n=10)
but it gives out following output
Output:
0123456789
I need to clear the privious line instead of printing 
how to fix this error.I need to write over integers 0 to 9


RE: what's wrong with "\033[F" and "\033[A" - wavic - Oct-20-2018

First, you have to be sure that your terminal emulator will recognize these escape sequences. Test it. I don't know what [K and [F do but here is what I get:

victor@jerry ~ $ for n in seq 10; do echo $n; echo -e "\033[F"; echo -e "\033[K"; done
seq

10

victor@jerry ~ $ 
What do you want to achieve?


RE: what's wrong with "\033[F" and "\033[K" - kavindu - Oct-20-2018

wavic ,it doesn't work correctly.I tested it but after doing that it doesn't print any thing for 10 seconds and after that it gives the privious result.
and how to undone the previous changes


RE: what's wrong with "\033[F" and "\033[A" - wavic - Oct-20-2018

If you want to print every number in the same line there is a way to do it in Python.

for n in range(10):
    print('\r', n, end='')
    time.sleep(1)
First, we print a carriage return character and the number. By default, print adds a new line character to everything it prints out. The last argument tells print the character to the end. In this case this is an empty string instead of new line.


RE: what's wrong with "\033[F" and "\033[A" - snippsat - Oct-20-2018

To make it more flexible string formatting f-string and no left space.
import time

for i in range(10):
    time.sleep(1)
    print(f'{i}\r', end='')
New screensaver Cool
cmd will have problem,cmder work fine Windows as most Terminals on Linux.
import time

for i in range(10):
    time.sleep(1)
    print(f'{i*999999}\r', end=' \U0001f604 \U0001f618 \U0001f375 ---> ' * i)
[Image: 1SiBSj.jpg]