0

Some Background?

I am not using the traditional python IDLE, but Anaconda Spyder (3.8) on a Windows 10 machine. I am looking for a script that clears the screen when called to do so. I have looked it up, but the results are more or less jumbled. I plan to use the script in Spyder and execute it using the IPython Console OR the Anaconda Prompt. I am looking for the smallest, simplest code that is available.

Some Specifications?

Script Editor - Anaconda Spyder (3.8)

IDLE - IPython || Anaconda Powershell

Script - Basic and simple

Okay, that was it for some Backstage Stuff, but here's the real question - How do I clear the console using a Python Script?

P.S. This question is NOT THE SAME as Any way to clear python's IDLE window? .

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • not all consoles can clear background. Real console/terminal (shell) like `cmd.exe` in Window and `terminal` in Linux may have method to clear it but tools like IDE may use pseudo-terminal (they only emulate terminal) which doesn't have method to clear it. – furas Oct 17 '20 at 10:21
  • @furas are IPython Consoles included in the category that can't? – Infinity Milestone Oct 17 '20 at 10:25
  • I don't know but I would expect that it can't clear screen. Frankly, I don't like when program/script clear screen because I can't see previous values and compare with current values. – furas Oct 17 '20 at 10:43
  • BTW: if you run `IPython` directly in shell/bash in Linux then you can use `print('\033c', end='')` which sends code `'\033c'` to `bash` and `bash` clears screen. But if you run `IPython` in `IDLE` then `IDLE` doesn't use `bash`/`powershell` but it catch text from `IPython` and display it in directly in tkinter's widget `Text` - but `Text` treats it as normal text `\033c`. Clearing screen is not part of `IPython` but it is functionality in shell like `bash`, `powershell`, `cmd.exe`. – furas Oct 18 '20 at 00:37
  • I found that `IPython` has magic command `%clear` which can clear it - at least it works for me in `IPython` in `Spyder 4.1.5` in Linux. But maybe it executes `os.system('clear')` or something similar to clear it. – furas Oct 18 '20 at 00:54
  • @furas, does the '%clear' command work when used in script? I am using a Windows machine (as in the question) so i need to check if it works in Windows as well. BTW : Using the 'IPython' console in 'Spyder', even 'cls' works. But it gives an error when used in script. – Infinity Milestone Oct 18 '20 at 03:35
  • `%clear` is 'magic command; which works only in IPython interactive mode. When you run script then it doesn't use interactive mode (but only pure Python) and magic commands will not works. You may try to check source code for `IPython` and maybe you find out what code execute IPython when you use `%clear` in interactive mode. – furas Oct 20 '20 at 08:35
  • @furas Thanks! will definitely try that! – Infinity Milestone Oct 20 '20 at 12:14

1 Answers1

1
import os
os.system('clear') 

Is a good way in linux

or

os.system('cls')

in windows

Divyessh
  • 2,540
  • 1
  • 7
  • 24