0

I want to print a line and replace that line with another text. Example code:

import os
import time
lst = ["|","/","-","\\"]
num = 0
for i in range(500):
    os.system("cls") #i don't want to clear the whole terminal window.
    print(lst[num])
    if num == 3:
        num = 0
    else:
        num = num + 1
    time.sleep(0.2)

just like when we install a tar.gz file using pip while building the package's setup.py it prints "building setup.py ,|,-,\" without clearing the console..

I have seen many posts regarding this.. but they all don't have a good answer..

SPECS:
OS: Windows 7 SP1
Python: Python 3.8.10
Arch: x86 (32-bit)
  • Check this [thread](https://stackoverflow.com/questions/44565704/how-to-clear-only-last-one-line-in-python-output-console), or this [one](https://stackoverflow.com/questions/6169217/replace-console-output-in-python), or even this [one](https://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console). – rawrex May 27 '21 at 05:28

1 Answers1

0

You can use \r (carriage return). Also you don't need num as en extra variable to index lst you can use existing i to index lst

import time
lst = ["|","/","-","\\"]
num = 0
for i in range(500):
    print(lst[i % 4], end="\r")     # i will be between 0 and 3 both inclusive
    time.sleep(0.2)
dottomato
  • 81
  • 1
  • 2