1

I want to know how to refresh the console of my program as if it was just started. Let's say that my code consists of an infinite loop and it has multiple instances of the print() function within itself, I want, every time that loops returns to its start, all the new data whether there is some change or not to get outputted on the same place of the data that has been outputted the last time. I have been reading about similar problems others have posted and the answers usually revolve around the idea of using \r, when I do that, however, it's always messy and the strings are either printed halfway or there are missing characters. On Replit there is a module called "replit" and there is a function there called clear() that basically performs what I need, but I don't seem to find it when I am using PyCharm, which means that it is perhaps something that works exclusively within the Replit environment. So I am asking, is there something similar in the standard python library that I can use? Thanks

Jake
  • 13
  • 4
  • 1
    If you only have one line, `\r` is the right answer, but you may need to `print( '\r' + ' '*80 + '\r')` to clear out the old data. If you want complete screen control, you may want to look at `curses`. – Tim Roberts Feb 17 '22 at 19:13

2 Answers2

0

You can use:

import os
command = 'cls' #for windows
os.system(command)

example:

print('hi')
os.system(command)
print('hi')

Output:

hi

For windows you need:

command = 'cls'

For all others it is:

command = 'clear'

To account for any OS you could use:

import os

def clearConsole():
    command = 'clear'
    if os.name in ('nt', 'dos'):  # If computer is running windows use cls
        command = 'cls'
    os.system(command)

clearConsole()
Eli Harold
  • 2,280
  • 1
  • 3
  • 22
  • You can automate the choice by checking `if os.name == 'posix'` more details: https://stackoverflow.com/a/1857/10302746 – Lev M. Feb 17 '22 at 19:30
  • Yes, but I thought that was slightly out of the question's scope, I will add an example of that. – Eli Harold Feb 17 '22 at 19:35
  • Thank you, sir, I was actually trying that approach but it wasn't working, or at least that's what I was thinking, turns out that windows and PyCharm have different ways of interpreting the instructions and as soon as I run the program through command prompt it worked properly. – Jake Feb 18 '22 at 17:16
  • @Jake interesting, I wonder why it does not work through your IDE, does it not have the actual console output (I have never used PyCharm)? I use Visual Studio Code and it works within the integrated terminal for me. – Eli Harold Feb 18 '22 at 18:21
0

There is nothing standard in Python to do it, because Python is not aware of whatever console you are using.

When you call print it is actually writing to a file called "standard output".

It can go to a console if you are running your program in a console (like windows cmd, Linux or Mac OS terminal app, or whatever PyCharm uses).

But it can also be redirected to a regular file by the user of your program.

So there is no standard way.

\r is "carriage return" character. On consoles that respect it, it will set your output position to the beginning of the current line, but will not erase any text already printed on that line (usually).

One way to print text in specific places on the screen is PyCurses.

It supports many consoles and figures out which one you are using automatically.

You can do something like this:

import curses

stdscr = curses.initscr()
stdscr.addstr(x, y, "my string")

By using the addstr isntead of print, you can choose the exact position the text will appear, with X and Y coordinates (first two parameters).

Read the documentation for more ways to manipulate text display with this library.

Lev M.
  • 6,088
  • 1
  • 10
  • 23