-1

This is ofcourse a duplicate question but I still did not get any answer...

Here the duplicate question click here

import time

print("The progress is going on as below...")

for i in range(1,6):
    print("\r" + "Done - " + str(i))
    print("\r" + "Left - " + str(5 - i))
    time.sleep(2)

I want to get the output as :

Done - 1
Left - 4

and again overwriting the 1 and 4 I want...

Done - 2
Left - 3

and again by over writing...

Done - 3
Left - 2

and so on.... I want all this to be overwritten.. in the 2 lines everytime while loops executes...

I want partial screen cleaning only those 2 lines... Not complete screen cleaning nor running multiple blank lines... Hope I am clear. Anyway possible ?

Kindly someone help...

This question is duplicate I accept but I could not find the solution for it... Please don't mark it down I just need help...

Please.

Abhi
  • 1,080
  • 1
  • 7
  • 21
  • Possible duplicate of [Python - Rewrite multiple lines in the Console](https://stackoverflow.com/questions/6840420/python-rewrite-multiple-lines-in-the-console) – Georgy Feb 20 '18 at 18:55
  • 1
    Possible duplicate of [Text Progress Bar in the Console](https://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console) – GSazheniuk Feb 20 '18 at 18:55

7 Answers7

2

This is the library you're looking for: https://docs.python.org/2/howto/curses.html

import curses, time
stdscr = curses.initscr()
for i in range(0, 5):
    stdscr.addstr(0, 0, '{0}'.format(i))
    stdscr.refresh()
    time.sleep(1)
curses.endwin()

Give something like this a try, it'll print numbers 0-4 in the upper left corner of your terminal.

EDIT:

here's the exact code you're looking for

import curses, time

stdscr = curses.initscr()
for i in range(1,6):
    stdscr.addstr(0, 0, "Done - " + str(i))
    stdscr.addstr(1, 0, "Left - " + str(5 - i))
    stdscr.refresh()
    time.sleep(2)

curses.endwin()
Steve
  • 939
  • 1
  • 6
  • 20
  • Will give it a try sir and let you know if its working or not ^_^ – Abhi Feb 20 '18 at 19:13
  • I just read your question again, is there other text on the screen? This solution doesn't clear the screen but it does put the lines in an absolute position. If you need something more dynamic that can be achieved with the same library but will take a little more work. – Steve Feb 20 '18 at 19:18
  • Also, curses only updates sections of the screen that have changed, so you might get some "after images" if you add strings of varying size on top of one another. – Steve Feb 20 '18 at 19:25
1
from curses import wrapper
import time

def main(stdscr):
   # Clear screen
   stdscr.clear()

   for i in range(0, 5):
      print("Done - %d" % i)
      print("Left - %d" % (5 - i))
      time.sleep(2)
      stdscr.clear()
      stdscr.refresh()
   print('press any key ...')
   stdscr.getkey()

wrapper(main)
0

Hope this solves your problem. Let me know if you have any doubts.

import time
import os
clear = lambda: os.system('cls')
clear()
for i in range(1,6):
    print("\r" + "Done - " + str(i))
    print("\r" + "Left - " + str(5 - i))
    time.sleep(2)
    if i!=5:
        clear()
    else:
        exit()
Abhisek Roy
  • 582
  • 12
  • 31
  • No brother I thought of this too but... I already have some printed statements in the window so I can't completely clear the screen... :( – Abhi Feb 20 '18 at 18:55
  • Okay. Partial screen cleaning. You should mention that in the description. – Abhisek Roy Feb 20 '18 at 18:58
0

The reason it's not working for you is print() automatically adds a newline \n to each line. Add end='' to the print call to override that.

print("\r" + "Done - " + str(i), end='')

BingsF
  • 1,269
  • 10
  • 15
  • I see from other answers you're on Windows. This answer works on Linux (or any POSIX-based environment) – BingsF Feb 21 '18 at 00:16
0

After you print, the text is pretty much permanent. Only way you could "change" what it says is to reprint. just run this code before reprinting and it should clear the screen.

print "\n" * 100

Though you could put this in a function:

def cls(): print "\n" * 100

And then call it when needed as cls()

Tim Lee
  • 358
  • 2
  • 8
0

This does the trick, but it only works with 1 line. for multiple lines you will have to use one of the other solutions like clear output screen.

import time

for i in range(1,6):
    print("\r" + "Done - " + str(i) + " pending - " + str(5 - i), end='')
    time.sleep(2)

Final output:

Done - 5 pending - 0
Vikash Singh
  • 13,213
  • 8
  • 40
  • 70
0

You may use sys.stdout.write and sys.stdout.flush to achieve this. Here "\033[F" moves the cursor up one line (which is an ANSI Escape Sequence). It works on POSIX based machines. (PS: It won't work on older versions windows as windows and DOS as they were not supporting ANSI)

import sys

for i in range(1,6):
    sys.stdout.write("\033[FDone - " + str(i))
    sys.stdout.write("\nLeft - " + str(5 - i))
    sys.stdout.flush()
    time.sleep(2)

Sample Run:

Done - 1
Left - 4

# after 2 seconds (last two lines replaced with)
Done - 2
Left - 3

# after 2 more seconds (last two lines replaced with)
Done - 3
Left - 2

# after 2 more seconds (last two lines replaced with)
Done - 4
Left - 1

# after 2 more seconds (last two lines replaced with)
Done - 5
Left - 0
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126