10

My problem is, that I want to be able to overwrite/clear previous printed line in python console. This question has been asked many times (Python - Remove and Replace Printed items for example), however with the very same code that is (the answer marked as correct, for me prints out nothing at all):

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 googled a lot, nothing works really. It either prints out nothing or just does not overwrite.

If that helps, I am running windows 8.1 and Python 3.51. Running the code trough cmd doesn't affect anything.

Also, adding sys.stdout.flush() does not help.

Community
  • 1
  • 1
czyngis
  • 413
  • 2
  • 5
  • 18
  • Did you check: http://stackoverflow.com/questions/27575929/ansi-escape-sequences-arent-printed-to-stdout-on-windows – Zorgmorduk Apr 09 '16 at 17:57
  • Sequences like `\033[F` must be supported by the terminal (see `termcap(3)`). AFAIK windows has always had very poor support for terminal capabilities – Andrea Corbellini Apr 09 '16 at 18:12

4 Answers4

6

You need to run your program from the command line, not from within IDLE.

Then, this should work:

import sys
import time

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

The \r goes to the beginning of the line. So you have to make sure the string you print is at least as long as the one before. Otherwise, you will see parts of the previous print.

Was'
  • 496
  • 4
  • 21
Mike Müller
  • 82,630
  • 20
  • 166
  • 161
4

You're trying to use ANSI escape sequences to move the cursor. Windows doesn't support those by default. To enable them, you could install the colorama module with pip install colorama in your terminal, then in Python:

import colorama
colorama.init()

If you've upgraded to Windows 10, you can enable support with this instead:

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

(Source: https://stackoverflow.com/a/36760881/6379747)

ForgottenUmbrella
  • 1,052
  • 14
  • 14
2

Updated answer for python 3.4+

Since print function has line ending and line flush arguments, you can do with this way.

text = str()
for i in range(10):
    text = "Loading{}".format("." * i)
    print(text, end="\r", flush=True)
print(" " * len(text), end="\r")

I could not find more elegant solution for cleaning the line at the end. If anybody knows, please share with me.

Sencer H.
  • 1,201
  • 1
  • 13
  • 35
0

Otherwise if you just want to render, you can do,

import sys
import time

# point by point
msg = "Loading"

print(msg, end="")

for _ in range(10):
    print(end=".")
    sys.stdout.flush()
    time.sleep(1)
print()

# Or all char by char
i = 10
msg = "Loading" + "." * i

for char in msg:
    print(end=char)
    sys.stdout.flush()
    time.sleep(1)
print()

I hope to help you. Thank's

Ridoine12
  • 11
  • 2