+ 1
How to clear the screen in python terminal ?
I need help
7 ответов
+ 1
>>> import os
>>> os.system('cls')
+ 9
Don't answer your own question and mark it as best.
+ 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)
+ 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.
+ 2
@~ swim ~ :D
+ 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
+ 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.