Python Forum
Overwrite last printed line - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Overwrite last printed line (/thread-7367.html)



Overwrite last printed line - DeGerlash - Jan-07-2018

Hi everyone,

I'm quite new to Python programming, which is why this thread is located in the Homework section: it's not because of a teacher that I'm restricted in my use of syntax, it's because of my basic knowledge.

I'm trying to program a clock to learn how to make a piece of code overwrite the last line I printed out to the display, to simulate a clock. I've been doing some research (the internet is stuffed with threads about this, I'm sorry), but everyone justs keeps going on about this infamous '\r' - thing, which is the return carriage, I suppose, and which should erase the last line and print the next line in place of the last line. On my computer, that is not what it does though: it just prints the next line right besides the previous line, resulting in some sort of junk-line of concatenated print-lines. So I found out about this 'flush'-thing that should clear your last line, but instead of clearing, it just does the exact same as if it weren't there (even if I set is to 'False'). Is this related to the type of computer I have? I found it was device-specific, but I found that quite strange.

Here's the failing code:
import time
x=0
for x in range(0,20):
    print(time.ctime(), end="\r", flush=True)
    time.sleep(1)
And here's what it results:
>>> %Run joke.py
Sun Jan 7 11:40:47 2018Sun Jan 7 11:40:48 2018Sun Jan 7 11:40:49 2018Sun Jan 7 11:40:50 2018Sun Jan 7 11:40:51 2018Sun Jan 7 11:40:52 2018Sun Jan 7 11:40:53 2018Sun Jan 7 11:40:54 2018Sun Jan 7 11:40:55 2018Sun Jan 7 11:40:56 2018Sun Jan 7 11:40:57 2018Sun Jan 7 11:40:58 2018Sun Jan 7 11:40:59 2018Sun Jan 7 11:41:00 2018Sun Jan 7 11:41:01 2018Sun Jan 7 11:41:02 2018Sun Jan 7 11:41:03 2018Sun Jan 7 11:41:04 2018Sun Jan 7 11:41:05 2018Sun Jan 7 11:41:06 2018


RE: Overwrite last printed line - Gribouillis - Jan-07-2018

You said in another thread that you are running python code in the Thonny IDE. As far as I know, the '\r' method works only in a terminal or console (such as a cmd tool in windows). It will most likely not work in an IDE's python shell. In fact, it may be impossible to overwrite the last line in the Thonny IDE.


RE: Overwrite last printed line - wavic - Jan-07-2018

You do not need the flush parameter.

It works for me.
In [1]: for i in range(10): # stupid mistake :D
   ...:     print("i", end='\r')
   ...:     
i
In [2]: for i in range(10):
   ...:     print(i, end='\r')
   ...:     
9
In [3]: from time import sleep

In [4]: for i in range(10):
   ...:     print(i, end='\r')
   ...:     sleep(0.5)
   ...:     
9
In [5]: from time import ctime

In [6]: for i in range(10):
   ...:     print(ctime(), end='\r')
   ...:     sleep(0.5)
   ...:     
Sun Jan  7 13:30:15 2018
In [7]: 



RE: Overwrite last printed line - DeGerlash - Jan-07-2018

I'm so sorry but i can't seem to fix the last code :D what do I need to put where?


RE: Overwrite last printed line - Gribouillis - Jan-07-2018

@wavic It works for you, but you are not in the Thonny IDE, are you?


RE: Overwrite last printed line - snippsat - Jan-07-2018

In Thonny IDE it will not overwrite,i tried it.
@DeGerlash run script in command line(cmd),then it will work.

It do work in stand alone REPL from command line like ptpython(what i use) or IPYthon.


RE: Overwrite last printed line - wavic - Jan-07-2018

Yes, I do not have Thonny installed.
What I am saying is that the issue is not in the code.


RE: Overwrite last printed line - DeGerlash - Jan-07-2018

But in cmd, my program shuts down the minute it's done. How do I stop this? Without using time.sleep(10), as I am unable to do anything then.


RE: Overwrite last printed line - wavic - Jan-07-2018

You want to keep counting?

Just replace the for loop with a while loop:
while True:
    print(time.ctime(), end="\r")
    time.sleep(1)
This will print the time until you press ctrl+c


RE: Overwrite last printed line - snippsat - Jan-07-2018

(Jan-07-2018, 04:20 PM)DeGerlash Wrote: But in cmd, my program shuts down the minute it's done.
If you start cmd first it will stay.
I mean you cd into folder with script and do python my_script.py.

DoubleClick to run,and want window to stay.
import time

for x in range(0,20):
    print(time.ctime(), end="\r", flush=True)
    time.sleep(1)

input('\n----- Press Enter to quit -----')