How to clear screen in python?
Most of the time, while working with python interactive shell/terminal (not a console), we end up with a messy output and want to clear the screen for some reason.
In an interactive shell/terminal, we can simply use
ctrl+l
But, what if we want to clear the screen while running a python script.
Unfortunately, there’s no built-in keyword or function/method to clear the screen. So, we do it on our own.
We can use ANSI escape sequence but these are not portable and might not produce desired output.
print(chr(27)+'[2j')
print('\033c')
print('\x1bc')
So, here’s what we’re going to do in our script:
- From os import system.
- Define a function.
- Make a system call with ‘clear’ in Linux and ‘cls’ in Windows as an argument.
- Store the returned value in an underscore or whatever variable you want (an underscore is used because python shell always stores its last output in an underscore).
- Call the function we defined.
# import only system from os from os import system, name # import sleep to show output for some time period from time import sleep # define our clear function def clear(): # for windows if name == 'nt': _ = system('cls') # for mac and linux(here, os.name is 'posix') else: _ = system('clear') # print out some text print('hello geeks\n'*10) # sleep for 2 seconds after printing output sleep(2) # now call function we defined above clear() |
NOTE: You can also only “import os” instead of “from os import system” but with that, you have to change system(‘clear’) to os.system(‘clear’).
Another way to accomplish this is using subprocess module.
# import call method from subprocess module from subprocess import call # import sleep to show output for some time period from time import sleep # define clear function def clear(): # check and make call for specific operating system _ = call('clear' if os.name =='posix' else 'cls') print('hello geeks\n'*10) # sleep for 2 seconds after printing output sleep(2) # now call function we defined above clear() |
Recommended Posts:
- set clear() in python
- Getting screen's height and width using Tkinter | Python
- Python Dictionary clear()
- Python | How to download windows lock-screen wallpapers
- Different ways to clear a list in Python
- Python | Multiple Sliders widgets Controlling Background Screen or WindowColor in Kivy
- Python PIL | ImageChops.screen() and ImageChops.offset() method
- CSS | clear Property
- multimap clear() function in C++ STL
- Reading Python File-Like Objects from C | Python
- Important differences between Python 2.x and Python 3.x with examples
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



