How to clear Windows Python interpreter screen?

Asked

Viewed 3,155 times

0

I’m starting to study Python (version 3), doing several syntax tests using the interpreter for Windows. However, I would like to clear the commands I have already executed from the screen as the scroll bar grows. I tried typing clear, clean, CTRL+L, but none of it worked.

1 answer

2


Based on these sources (fonte1, fonte2), being windows you can do:

import os
os.system('cls')

If it were linux it would be:

import os
os.system('clear')

I didn’t test it, but I believe it will work

  • It worked. But I found it a little verbose for a simple clear command...

  • It’s true too, but it’s windows :P, I’ll try to find some alternative but I doubt there is a less verbose

  • There really isn’t. Because the executed command belongs to the interpreter, it is not native to Python, so you should use the library os. Also remember that Python is portable: the same code works on both Windows and Linux, or another operating system, so it’s interesting to do something like os.system('cls' if os.name=='nt' else 'clear'), for cls works on Windows, while clear works on Linux.

  • I added this alternative (being linux) to the answer, obgado @Andersoncarloswoss

Browser other questions tagged

You are not signed in. Login or sign up in order to post.