Clear screen in shell

2019-01-10 06:39发布

问题:

Just a quick question:
How do you clear the screen in shell? I've seen ways like:

import os
os.system('cls')

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

回答1:

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

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

To prevent '0' from showing on top of the window, replace the 2nd line with:

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

For linux, you must replace cls command with clear

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


回答2:

What about the shortcut CTRL+L?

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



回答3:

import os

os.system('cls')  # For Windows
os.system('clear')  # For Linux/OS X


回答4:

The sort of thing that you are looking for is to 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

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

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

If you don't you will get all sort of strange behaviour. To ensure that this is always done I would suggest using the atexit module, something like:

import atexit

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

Will probably do nicely.



回答5:

In addition to being an all-around great CLI library, click also provides a platform-agnostic clear() function:

import click
click.clear()


回答6:

An easier way to clear a screen while in python is to use Ctrl + L though it works for the shell as well as other programs.



回答7:

using windows 10 and pyhton3.5 i have tested many codes and nothing helped me more than this:

First define a simple function, this funtion will print 50 newlines;(the number 50 will depend on how many lines you can see on your screen, so you can change this number)

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

then just call it as many times as you want or need

cls()


回答8:

This function works in any OS (Unix, Linux, OS X, and Windows)
Python 2 and Python 3

from platform import system as system_name # Returns the system/OS name
from os import system as system_call       # Execute a shell command

def clear_screen():
    """
    Clears the terminal screen.
    """

    # Clear command as function of OS
    command = "-cls" if system_name().lower()=="windows" else "clear"

    # Action
    system_call(command)

In windows the command is cls, in unix-like systems the command is clear.
platform.system() returns the platform name. Ex. 'Darwin' in OS X.
os.system() performs a system call. Ex. os.system('ls -al')



回答9:

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



回答10:

Here are some options that you can use on Windows

First option:

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

>>> cls()

Second option:

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

>>> cls()

Third option if you are in Python REPL window:

Ctrl+L


回答11:

Command+K works fine in OSX to clear screen.

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



回答12:

import curses
stdscr = curses.initscr()
stdscr.clear()


回答13:

Subprocess allows you to call "cls" for Shell.

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

That's as simple as I can make it. Hope it works for you!



回答14:

  1. you can Use Window Or Linux Os

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

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


回答15:

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