2

I want to print the current time on screen in hours and minutes that updates automatically without making a new line, as well as print another line with the seconds that also updates automatically on a second line. Thanks in advance! This is what I have so far:

import time
from datetime import datetime

while True:
    now = datetime.now()
    current_time = now.strftime("%I:%M %p")
    current_second = now.strftime("%S")
    print("\r" + "Clock: ", current_time, end= ' ')
    print("\r" + "\n" + "Seconds: ", current_second, end=' ')
    time.sleep(1)

This is what the output looks like: Output

The seconds out like they should, but the time does not

  • 1
    `print` sends bytes to a stream. Most terminals have some control characters to control where the characters print on the screen, but that will be dependent on the terminal or emulation the program is running under. – Garr Godfrey Sep 07 '21 at 22:22
  • 1
    I am not quite sure that I follow... – Tasman Orkiszewski Sep 07 '21 at 22:25
  • 1
    No, I know how to print on the same line, but I want the seconds on a separate and for both lines to update automatically – Tasman Orkiszewski Sep 07 '21 at 22:28
  • @TasmanOrkiszewski What about [this answer](https://stackoverflow.com/a/39455032/1717828)? Does that take care of it for you? – user1717828 Sep 07 '21 at 22:31
  • Maybe look at this? https://stackoverflow.com/questions/6840420/rewrite-multiple-lines-in-the-console – kiran_isaac Sep 07 '21 at 22:36
  • @user1717828 this answer looks more like what I need...but I am using pycharm and for some reason it still prints both on separate lines... – Tasman Orkiszewski Sep 07 '21 at 22:40
  • 1
    @TasmanOrkiszewski Sorry, I don't know what to tell you. I just ran it in IPython and the regular terminal and it works just like in the GIF. It sounds like you might need to update your problem with your IDE specified and include any reason why general solutions wouldn't work there. – user1717828 Sep 07 '21 at 22:45
  • @user1717828 this is what happens when I try to run it in command prompt. I am almost definitely missing something simple but this is for a school project to I am an extreme beginner https://imgur.com/a/28HndVH – Tasman Orkiszewski Sep 07 '21 at 22:56
  • @JonSG That script I have the same problem where it still prints on new lines, which makes me certain I am missing something simple... – Tasman Orkiszewski Sep 07 '21 at 22:58
  • I think in combination, these answer your question. https://stackoverflow.com/questions/2084508/clear-terminal-in-python and https://stackoverflow.com/questions/5598181/multiple-prints-on-the-same-line-in-python – JonSG Sep 07 '21 at 23:36

3 Answers3

1

Try clearing the console with a clear method. Define it like this:

from os import system, name

if name == "nt":
    clear = lambda: system("cls")
else:
    clear = lambda: system("clear")

and then just call it every loop

Rhuamer
  • 46
  • 3
1

Combining @Rhuamer's answer with your code, you can do something like:

import subprocess
import sys
import time
from datetime import datetime

clear = "cls" if sys.platform == "win32" else "clear"
while True:
    now = datetime.now()
    current_time = now.strftime("%I:%M %p")
    current_second = now.strftime("%S")
    print(f"\rClock: {current_time}", flush=True, end="\n")
    print(f"Seconds: {current_second}", flush=True, end="")
    time.sleep(1)
    subprocess.run(clear, shell=True)
Tes3awy
  • 2,166
  • 5
  • 29
  • 51
  • I get these errors when I try to run this https://imgur.com/a/znNOOdO – Tasman Orkiszewski Sep 07 '21 at 23:03
  • @TasmanOrkiszewski What shell are you using on your Windows PC? – Tes3awy Sep 07 '21 at 23:04
  • I am just using pycharm...in python class we were using web based python but that could not import all of the modules that I need so I switched to pycharm...I thought that that was all I needed – Tasman Orkiszewski Sep 07 '21 at 23:06
  • @TasmanOrkiszewski Please check the update, I added the `shell=True` parameter. – Tes3awy Sep 07 '21 at 23:07
  • Now it works for running something but it does not overwrite the hours and minutes, only the seconds. Is this another shell problem? https://imgur.com/a/wFHbtas – Tasman Orkiszewski Sep 07 '21 at 23:10
  • @TasmanOrkiszewski It works on GitBash, Command Prompt, and PowerShell in VSCode integrated terminal. I think there is a problem with PyCharm Terminal Emulator! Can you please try it outside PyCharm? – Tes3awy Sep 07 '21 at 23:14
  • aha! it works in terminal! are there any programs like pycharm with an easy gui to script in that will work do you know? Because coding in command prompt is kind of complicated for me... – Tasman Orkiszewski Sep 07 '21 at 23:29
  • @TasmanOrkiszewski Yes, Visual Studio Code is very easy you can try it out. – Tes3awy Sep 07 '21 at 23:32
1

The heart of this question is based around how to clear the console and there are a number of good answers here Clear terminal in Python and I recommend you explore it. The other part of the question deals with writing on multiple lines and so I feel this is not quite a duplicate.

I think the easiest way to get what you seek might be to use an f-string and simply do:

import time
from datetime import datetime

while True:
    now = datetime.now()
    current_time = now.strftime("%I:%M %p")
    current_second = now.strftime("%S")
    print(f"\033cClock: {current_time}\nSeconds: {current_second}")
    time.sleep(1)

If that does not quite work correctly, there are other more slightly extended escape codes you can explore such as \033c\033[3J. I have tested the \033c on a few terminals with success though.

If after reviewing that other question, you feel this is a duplicate, just let me know and I will remove this answer and you can close the question.

JonSG
  • 10,542
  • 2
  • 25
  • 36
  • 1
    I like your answer and it works on all terminals I use except for GitBash using `Windows default console window` option but works with GitBash installed using the `use MinTTY (the default terminal of MSYS2)` option. – Tes3awy Sep 07 '21 at 23:45
  • 1
    Ya, this probably only works on a terminal that supports ansi escapes, so like everything other than cmd and powershell without registry entries – JonSG Sep 08 '21 at 00:02