2

So i am currently trying to make something that will print . and remove it then print .. and also remove it then print ... When i tried using sys module to remove the prevous text this was the output: lol [Ktest so it basically adds a [K to the next line. I also tried using another method so instead of removing the prevous text it would just add onto it like:

import time
print("lol",end="")
time.sleep(1)
print("test")

it did work in IDLE but when i tried to use it by opening the file in the command promt it waited for 1 second and then just gave loltest without any delay between them. So nothing i found in the internet worked for me.

  • 1
    You might be looking for the Carriage Return, `\r`, https://www.codespeedy.com/how-does-carriage-return-work-in-python/ – iScripters Nov 28 '21 at 11:46
  • 1
    Does this answer your question? [How to overwrite the previous print to stdout in python?](https://stackoverflow.com/questions/5419389/how-to-overwrite-the-previous-print-to-stdout-in-python) – Gino Mempin Nov 28 '21 at 11:54
  • Does this answer your question? [How to clear the interpreter console?](https://stackoverflow.com/questions/517970/how-to-clear-the-interpreter-console) – Sekomer Nov 28 '21 at 11:57
  • If this is meant to indicate progress of an operation, there are also progress bar libraries on pypi; if you find yourself writing more than a couple of lines for this, consider using one of those? – Jiří Baum Apr 17 '22 at 14:21
  • To fix the "waited 1 second then printed everything" problem, you need to use `flush=True` on the first `print`, like this: `print("lol", end="", flush=True)` – Jiří Baum Apr 17 '22 at 14:31

5 Answers5

2

You may print with the keyword argument end to append the special character '\r' to the end of the line.

E.g.

import time
print(".", end='\r')
time.sleep(2)
print("..", end='\r')
time.sleep(2)
print("...", end='\r')
time.sleep(2)

'\r' is carriage return and will return to the start of the line in some terminals, from where you can overwrite the text you just printed. Note that the behaviour might differ between terminals though.

L.Grozinger
  • 2,280
  • 1
  • 11
  • 22
1

You can use the os module to execute shell commands.

To clear the terminal, command required in windows is cls and for unix its clear

import os
os.system('cls' if os.name == 'nt' else 'clear')

If you don't want to clear previous terminal outputs you can use flexibility of print function or the carriage return as others denoted.

for _ in range(3):
    print('.', end='')
    time.sleep(1)
Sekomer
  • 688
  • 1
  • 6
  • 24
1

To print over the prvious print, you can use end="\r.

import time

print("lol", end="\r")
time.sleep(1)
print("test")
for i in range(4):
    print("."*i, end="\r")
    time.sleep(1)

yannvm
  • 418
  • 1
  • 4
  • 12
0

You can do it with ANSI escape codes, like this:

import sys, time

clear_line = '\x1b[1K\r'

print("lol", end="")
sys.stdout.flush()  # to force printing the text above
time.sleep(1)
print(clear_line+"test")  # Now lol replaced with test

Please note that ANSI codes you should use depend on the environment where the program is executing (platform, terminal, etc.).

Update: you may want to see the built-in curses module.

Vlad Havriuk
  • 1,291
  • 17
  • 29
0

If you specifically want to print . then .. then ..., you don't need to remove the existing text; you can just print additional dots.

To make the dots actually appear one by one, you'll need to flush the buffers, using flush=True

import time

for _ in range(3):
  print('.', end='', flush=True)
  time.sleep(1)

print()

This has the advantage that it will work much more generally; almost any output method can do that, whereas ANSI codes or tricks with \r or clearing the screen depend on your hardware, operating system and various other things.

Jiří Baum
  • 6,697
  • 2
  • 17
  • 17