Python 在外壳中清除屏幕
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18937058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clear screen in shell
提问by josh
Just a quick question:
How do you clear the screen in shell?
I've seen ways like:
只是一个简单的问题:
你如何在 shell 中清除屏幕?我见过这样的方法:
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 :)
这只是打开 windows cmd,清除屏幕并关闭,但我希望清除 shell 窗口
(PS:我不知道这有帮助,但我使用的是 Python 3.3.2 版)
谢谢:)
采纳答案by GalHai
For OS X, you can use subprocess module and call 'cls' from shell:
对于 OS X,您可以使用 subprocess 模块并从 shell 调用“cls”:
import subprocess as sp
sp.call('cls',shell=True)
To prevent '0' from showing on top of the window, replace the 2nd line with:
要防止“0”显示在窗口顶部,请将第二行替换为:
tmp = sp.call('cls',shell=True)
For linux, you must replace cls
command with clear
对于 linux,您必须将cls
命令替换为clear
tmp = sp.call('clear',shell=True)
回答by Steve Barnes
The sort of thing that you are looking for is to be found in the curses module.
您正在寻找的东西可以在 curses 模块中找到。
i.e.
IE
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:
如果你不这样做,你会得到各种奇怪的行为。为了确保始终做到这一点,我建议使用 atexit 模块,例如:
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.
可能会做得很好。
回答by sl1ng5h0t
import curses
stdscr = curses.initscr()
stdscr.clear()
回答by Rajan Mandanka
import os
os.system('cls') # For Windows
os.system('clear') # For Linux/OS X
回答by Seth Connell
Subprocess allows you to call "cls" for Shell.
Subprocess 允许您为 Shell 调用“cls”。
import subprocess
cls = subprocess.call('cls',shell=True)
That's as simple as I can make it. Hope it works for you!
这就是我能做到的那样简单。希望这对你有用!
回答by BioinfoNerd
An easier way to clear a screen while in python is to use Ctrl+ Lthough it works for the shell as well as other programs.
在 python 中清除屏幕的一种更简单的方法是使用Ctrl+L虽然它适用于 shell 以及其他程序。
回答by HMS
If you are using linux terminal to access python, then cntrl+l is the best solution to clear screen
如果你使用linux终端访问python,那么cntrl+l是清屏的最佳方案
回答by Shafiq
What about the shortcut CTRL+L?
快捷键CTRL+L呢?
It works for all shells e.g. Python, Bash, MySQL, MATLAB, etc.
它适用于所有 shell,例如 Python、Bash、MySQL、MATLAB 等。
回答by Rohit Parghi
you can Use Window Or Linux Os
import os os.system('cls') os.system('clear')
you can use subprocess module
import subprocess as sp x=sp.call('cls',shell=True)
您可以使用 Window 或 Linux 操作系统
import os os.system('cls') os.system('clear')
您可以使用子流程模块
import subprocess as sp x=sp.call('cls',shell=True)
回答by Phich
os.system('cls')
works fine when I open them. It opens in cmd style.
os.system('cls')
当我打开它们时工作正常。它以 cmd 样式打开。