Python Forum
Overwrite last printed line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Overwrite last printed line
#1
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
Reply
#2
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.
Reply
#3
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]: 
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
I'm so sorry but i can't seem to fix the last code :D what do I need to put where?
Reply
#5
@wavic It works for you, but you are not in the Thonny IDE, are you?
Reply
#6
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.
Reply
#7
Yes, I do not have Thonny installed.
What I am saying is that the issue is not in the code.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
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.
Reply
#9
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
(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 -----')
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020