Overwrite / clear previous console

My problem is that I want to be able to overwrite / clear the previous print line in the python console. This question has been asked many times ( Python - remove and replace print elements ), but with the same code that (the answer is marked as correct, it doesn’t display anything for me):

for i in range(10):
    print("Loading" + "." * i)
    time.sleep(1)
    sys.stdout.write("\033[F") # Cursor up one line
    sys.stdout.write("\033[K") # Clear to the end of line

I get the output (in python IDLE):

Loading
[F[KLoading.
[F[KLoading..
[F[KLoading...
[F[KLoading....
[F[KLoading.....
[F[KLoading......
[F[KLoading.......
[F[KLoading........
[F[KLoading.........
[F[KLoading..........
[F[K

Any ideas? I google a lot, nothing works. It either does not print anything, or simply does not overwrite.

If this helps, I am running Windows 8.1 and Python 3.51. Running the cmd code trough has no effect.

In addition, the addition sys.stdout.flush()does not help.

+4
source share
2 answers

, IDLE.

:

import sys
import time

for i in range(10):
    sys.stdout.write("\r" + "Loading" + "." * i)
    time.sleep(1)
    sys.stdout.flush()
print()

\r . , , . . ​​

+1

escape- ANSI . Windows . , colorama pip install colorama , Python:

import colorama
colorama.init()

Windows 10, :

import ctypes
kernel32 = ctypes.windll.kernel32
kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)

(: fooobar.com/questions/837759/...)

+1

All Articles