Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir()
stuff, help() stuff
Here are two nice ways of doing that:
1.
import os
# Clear Windows command prompt.
if (os.name in ('ce', 'nt', 'dos')):
os.system('cls')
# Clear the Linux terminal.
elif ('posix' in os.name):
os.system('clear')
2.
import os
def clear():
if os.name == 'posix':
os.system('clear')
elif os.name in ('ce', 'nt', 'dos'):
os.system('cls')
clear()
I use iTerm and the native terminal app for Mac OS.
I just press ⌘ + k
The easiest way is to use os module
>>> import os
>>> clear = lambda: os.system('clear')
>>> clear()
This is the simplest thing you can do and it doesn't require any additional libraries. It will clear the screen and return >>>
to the top left corner.
print("\033[H\033[J")
Here's the definitive solution that merges all other answers. Features:
You can use it as you like:
>>> clear()
>>> -clear
>>> clear # <- but this will only work on a shell
You can import it as a module:
>>> from clear import clear
>>> -clear
You can call it as a script:
$ python clear.py
It is truly multiplatform; if it can't recognize your system
(ce
, nt
, dos
or posix
) it will fall back to printing blank lines.
You can download the [full] file here: https://gist.github.com/3130325
Or if you are just looking for the code:
class clear:
def __call__(self):
import os
if os.name==('ce','nt','dos'): os.system('cls')
elif os.name=='posix': os.system('clear')
else: print('\n'*120)
def __neg__(self): self()
def __repr__(self):
self();return ''
clear=clear()
for the mac user inside the python console type
import os
os.system('clear')
for windows
os.system('cls')