- 2
Clear console before executing python script
I want my python script to clear the console before it runs. I am using the Spyder3 IDE in linux. The following code is what I thought might work, but the iPhython console within Spyder3 does not clear when executing the script import os os.system('clear')
2 odpowiedzi
+ 4
"""
Same result can be done with '\33[2J' (same escape sequence with different notation ;)) and works on any OS, depending on terminal/console support of ANSI escape control chars (but clear screen seems widely supported)...
Anyway, in QPython, the behaviour isn't totally as expected: the screen is good cleared, but the cursor not moved at first line/column.
Simple workaround, is to add the escape sequence for explicitly move the cursor at 0,0 position... You can even define a reusable function to set the cursor at any x,y coordinates:
"""
def pos(y=0,x=0):
if x or y:
return '\33[{};{}f'.format(y,x)
else:
return '\33[f'
# and define a 'cls' variable to easiest handle them:
cls = '\33[2J'
# so you can use:
print(cls+pos(),end='')
"""
(don't forget to provide an empty string end line parameter, as default value is a new line char, wich will move your cursor at next line -- 1 instead of 0)
"""
- 1
In Linux you can use:
print('\x1b[2J')
I cannot confirm if this works in Windows or MacOS aswell. If it does, please comment.