+ 1

How to clear the screen in python terminal ?

I need help

6th Oct 2017, 11:54 AM
Boojelben Ismayil
Boojelben Ismayil  - avatar
10 Answers
+ 1
>>> import os >>> os.system('cls')
4th Oct 2017, 10:55 PM
Boojelben Ismayil
Boojelben Ismayil  - avatar
+ 9
Don't answer your own question and mark it as best.
11th Nov 2017, 7:56 AM
qwerty
qwerty - avatar
+ 6
Unfortunately, shelling out to os just clears the screen in a new process, which promptly exits. :/ If you want to clear the current output, which is attached to a console: # Windows, same as Ctrl-L (printer form feed: new sheet of paper): print (chr(12)) # Linux, OSX, Android (terminals): esc = chr(27) print(esc + '[2J' + esc + '[0;0H') Explanation: ANSI escape sequences. Escape [2J wipes the screen but leaves the cursor Escape [0;0H homes the cursor to position (0,0)
4th Oct 2017, 11:30 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
@~ swim ~ That would cause the new text to appear at the bottom of the screen. I mean, it's not that big of a deal, but just to let Boojelben know.
6th Oct 2017, 2:46 PM
LunarCoffee
LunarCoffee - avatar
+ 2
@~ swim ~ :D
6th Oct 2017, 4:16 PM
LunarCoffee
LunarCoffee - avatar
+ 1
if u are interested in learning Python check out this dude "The Xcode" he just started learning Python toturiles on YouTube . the link to his channel : https://m.youtube.com/channel/UCsinMWgI7pXJQGc0gwMXimg
4th Oct 2017, 6:18 PM
jay
+ 1
One additional detail: check that os.system() is not returning -1 (it is here) This could be for a few reasons: Unable to start command interpreter 'cls' is an internal (not an external) command (it may even be looking for cls.exe) spawning %COMSPEC% is blocked by group policy (it is on SoloLearn) But I believe anything other than 0 is an error.
5th Oct 2017, 12:19 AM
Kirk Schafer
Kirk Schafer - avatar