How to clear the interpreter console?

2018-12-31 08:56发布

Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.

Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same command several times. I'm wondering if, and how, to clear the Python interpreter console.

I've heard about doing a system call and either calling cls on Windows or clear on Linux, but I was hoping there was something I could command the interpreter itself to do.

Note: I'm running on Windows, so Ctrl+L doesn't work.

30条回答
萌妹纸的霸气范
2楼-- · 2018-12-31 09:19

I use iTerm and the native terminal app for Mac OS.

I just press ⌘ + k

查看更多
宁负流年不负卿
3楼-- · 2018-12-31 09:21

Quickest and easiest way without a doubt is Ctrl+L.

This is the same for OS X on the terminal.

查看更多
低头抚发
4楼-- · 2018-12-31 09:21

I'm new to python (really really new) and in one of the books I'm reading to get acquainted with the language they teach how to create this little function to clear the console of the visible backlog and past commands and prints:

Open shell / Create new document / Create function as follows:

def clear():
    print('\n' * 50)

Save it inside the lib folder in you python directory (mine is C:\Python33\Lib) Next time you nedd to clear your console just call the function with:

clear()

that's it. PS: you can name you function anyway you want. Iv' seen people using "wiper" "wipe" and variations.

查看更多
余生无你
5楼-- · 2018-12-31 09:22

just use this..

print '\n'*1000

查看更多
余生无你
6楼-- · 2018-12-31 09:22

I found the simplest way is just to close the window and run a module/script to reopen the shell.

查看更多
姐姐魅力值爆表
7楼-- · 2018-12-31 09:24

I am using Spyder (Python 2.7) and to clean the interpreter console I use either

%clear

that forces the command line to go to the top and I will not see the previous old commands.

or I click "option" on the Console environment and select "Restart kernel" that removes everything.

查看更多
登录 后发表回答