0

I am struggling to replace the print lines in windows CMD

for i in range(10):
    print ("Range: ", i)
    print ("Loading... ")
    sys.stdout.write("\033[F"*2)
    sys.stdout.write("\033[K")

The issue is visible here:

Screenshot in python 3

I want to replace the first line Range: 1 so how can I do it in python 3.x

kalalua
  • 35
  • 4
  • Replace by what? Also, ```print ("Range: ", i)``` is correct –  Aug 24 '21 at 06:40
  • Hey @Sujay as indicated in the question I want to replace the line `Range: 1` to `Range: 2` to `Range: 3` .......... so `i` will be replaced from the line on every change but I do not want to print it in next line every time, instead what I am trying is to replace the whole line which can be done in py2 easily – kalalua Aug 24 '21 at 06:44
  • If i got you correct, you can do ```x="Range: "``` before the loop and ```x+=str(i)``` in the loop instead of ```"Range: ", i``` –  Aug 24 '21 at 06:45
  • The behaviour of your code will be identical in Python 2.x and 3.x. What is the problem? –  Aug 24 '21 at 06:50
  • does [this](https://python-forum.io/thread-13538-post-61140.html#pid61140) answer you question (it is off-site tho), basically it uses the carriage return `"\r"` – Matiiss Aug 24 '21 at 06:56
  • Overwriting the last line is pretty easy. Doing it with a line before the last one is not as easy, and rather dependent on your environment. – Amadan Aug 24 '21 at 07:04
  • Exactly @Amadan that's the challange I want to solve – kalalua Aug 24 '21 at 07:05
  • @DarkKnight I have attached a screenshot if you wanna look again – kalalua Aug 24 '21 at 07:06
  • Your screenshot is more confusing than not, with irrelevant error messages and code that doesn't work. Please read [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question). Now, this is not really a Python question, but one about ANSI sequences; tag with [tag:ansi-escape]. Furthermore, I don't have a Windows handy, but as I said, when it comes to ANSI codes, environment matters: please tag with [tag:cmd] and the appropriate Windows version; on some, there may not be a solution. – Amadan Aug 24 '21 at 07:14

0 Answers0