Python Forum
what's wrong with "\033[F" and "\033[A"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what's wrong with "\033[F" and "\033[A"
#1
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
Reply
#2
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?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
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
Reply
#4
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
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]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,533 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  python gives wrong string length and wrong character thienson30 2 2,988 Oct-15-2019, 08:54 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020