Clear screen in shell

Just a quick question:
How to clean the screen in the shell? I have seen ways like:

import os os.system('cls') 

It just opens the cmd window, clears the screen and closes, but I want the shell window to be cleared
(PS: I don't know if this helps, but I am using Python version 3.3.2)
Thank:)

+59
python shell screen clear
Sep 21 '13 at 20:05
source share
16 answers

For OS X, you can use the subprocess module and call 'cls' from the shell:

 import subprocess as sp sp.call('cls',shell=True) 

In order not to show “0” on top of the window, replace the second line as follows:

 tmp = sp.call('cls',shell=True) 

For linux, you should replace the cls command with clear

 tmp = sp.call('clear',shell=True) 
+36
Nov 27 '13 at 16:03
source share

What about the CTRL + L shortcut?

It works for all shells, for example. Python, Bash, MySQL, MATLAB, etc.

+61
Aug 07 '15 at 6:56
source share
 import os os.system('cls') # For Windows os.system('clear') # For Linux/OS X 
+40
May 7 '14 at 11:43
source share

What you are looking for can be found in the curses module.

i.e.

 import curses # Get the module stdscr = curses.initscr() # initialise it stdscr.clear() # Clear the screen 

Important Note

It is important to remember that before any exit you need to reset the terminal to normal mode, this can be done with the following lines:

 curses.nocbreak() stdscr.keypad(0) curses.echo() curses.endwin() 

If you do not, you will get all the strange behavior. To always do this, I would suggest using the atexit module, for example:

 import atexit @atexit.register def goodbye(): """ Reset terminal from curses mode on exit """ curses.nocbreak() if stdscr: stdscr.keypad(0) curses.echo() curses.endwin() 

Most likely it will be good.

+11
Sep 21 '13 at 20:23
source share

In addition to the universal CLI library, click also provides a platform-independent clear() function:

 import click click.clear() 
+6
Nov 14 '17 at 22:20
source share

using windows 10 and pyhton3.5 , I checked a lot of codes and nothing helped me anymore:

First define a simple function, this function will print 50 lines of a new line (the number 50 will depend on how many lines you see on the screen so that you can change this number)

 def cls(): print ("\n" * 50) 

then just name it as many times as you want, or you need

 cls() 
+4
Apr 23 '16 at 0:43
source share

Below are some options that you can use in Windows.

First option:

 import os cls = lambda: os.system('cls') >>> cls() 

Second option:

 cls = lambda: print('\n' * 100) >>> cls() 

The third option, if you are in the Python REPL window:

 Ctrl+L 
+4
Jan 19 '17 at 19:34 on
source share

This feature works on any OS (Unix, Linux, macOS and Windows)
Python 2 and Python 3

 import platform # For getting the operating system name import subprocess # For executing a shell command def clear_screen(): """ Clears the terminal screen. """ # Clear command as function of OS command = "cls" if platform.system().lower()=="windows" else "clear" # Action return subprocess.call(command) == 0 

On windows, the cls command; on unix -l ike systems, the clear command.
platform.system() returns the platform name. Ex. 'Darwin' for macOS.
subprocess.call() makes a system call. Ex. subprocess.call(['ls','-l'])

+4
Mar 18 '17 at 17:13
source share

An easier way to clear the screen while in python is to use Ctrl + L , although it works with both the shell and other programs.

+3
Mar 25 '15 at 19:29
source share

If you use a linux terminal to access python, then cntrl + l is the best solution to clear the screen

+3
May 21 '15 at 10:08
source share

Command+K works fine on OSX to clear the screen.

Shift+Command+K to clear only the scroll buffer.

+2
Sep 20 '18 at 6:10
source share
 import curses stdscr = curses.initscr() stdscr.clear() 
+1
Sep 21 '13 at 21:22
source share

The subprocess allows you to call "cls" for Shell.

 import subprocess cls = subprocess.call('cls',shell=True) 

It is as simple as I can do it. Hope this works for you!

+1
Jan 09 '15 at 0:12
source share
  • you can use Window or Linux Os

     import os os.system('cls') os.system('clear') 
  • you can use the subprocess module

     import subprocess as sp x=sp.call('cls',shell=True) 
+1
Aug 18 '15 at 5:30
source share

os.system('cls') works fine when I open them. Opens in cmd style.

+1
01 Sep '15 at 17:50
source share

I use a class that just uses one of the above methods backstage ... I noticed that it works on Windows and Linux ... I like to use it though, because it's easier to type clear () instead of system ('clear' )) or os.system ('clear')

 pip3 install clear-screen from clear_screen import clear 

and then when you want to clean the shell:

 clear() 
0
Jan 28 '19 at 6:03
source share



All Articles