OP 22 March, 2021 - 04:03 AM
Let's talk about the do's and dont's about clearing a terminal.
Most people might be thinking, "I know how to clear a terminal! I'll just use the builtin library os!"
Which you NEVER want to do. I am referring to using the os.system command.
This is a very unstable, inefficient and bad way to clear a screen. So what's the best way? Hold your horses, let me tell you why it's a terrible way.
"os.system" creates an unnecessary shell that differs from the one Python uses. In terms, it is also vulnerable to shell-injection. And to top it all off,
it's a very inefficient and non-performant way of doing things. I've seen multiple applications that use it in a non-terminating loop (while True) and calling
a function that uses that command. For each call, a new shell is created that interacts with your system at a basic level (in most cases) to only call "cls" or "clear".
Think of it as this way, each function call is you eating and digesting an apple. You open your mouth (opening the shell), take a bite and wait until your body finishes
decomposing the apple (calling the command) and finally you digest it (closing the shell). Now imagine this happening tens of thousands of times. Not fun is it? And
it takes a long time as well! That's exactly how your computer feels, don't use "os.system" always use the module "subprocess" for dealing with system calls. Anyways,
let's get on with the tutorial.
How do you clear your screen if using "os.system" is bad? Simple.
Note: if you have any errors, make sure to type python -m pip install colorama and re-run the program. Also, if you are getting the SyntaxError: invalid character in identifier error, make sure when you copy and paste the code above, to re-indent it or delete all characters before the print and then hit tab. Should work fine after that (c.to does some weird stuff when you copy code from the site).
When you want to clear your screen, all you have to do is call the function clear_screen() which is immensely better than creating a shell to do it.
That's all,
LEAVE A LIKE IF YOU LEARNED ANYTHING
[/code]
Most people might be thinking, "I know how to clear a terminal! I'll just use the builtin library os!"
Which you NEVER want to do. I am referring to using the os.system command.
This is a very unstable, inefficient and bad way to clear a screen. So what's the best way? Hold your horses, let me tell you why it's a terrible way.
"os.system" creates an unnecessary shell that differs from the one Python uses. In terms, it is also vulnerable to shell-injection. And to top it all off,
it's a very inefficient and non-performant way of doing things. I've seen multiple applications that use it in a non-terminating loop (while True) and calling
a function that uses that command. For each call, a new shell is created that interacts with your system at a basic level (in most cases) to only call "cls" or "clear".
Think of it as this way, each function call is you eating and digesting an apple. You open your mouth (opening the shell), take a bite and wait until your body finishes
decomposing the apple (calling the command) and finally you digest it (closing the shell). Now imagine this happening tens of thousands of times. Not fun is it? And
it takes a long time as well! That's exactly how your computer feels, don't use "os.system" always use the module "subprocess" for dealing with system calls. Anyways,
let's get on with the tutorial.
How do you clear your screen if using "os.system" is bad? Simple.
Code:
import colorama
colorama.init()
def clear_screen():
print('\x1b[2J', end='')
Note: if you have any errors, make sure to type python -m pip install colorama and re-run the program. Also, if you are getting the SyntaxError: invalid character in identifier error, make sure when you copy and paste the code above, to re-indent it or delete all characters before the print and then hit tab. Should work fine after that (c.to does some weird stuff when you copy code from the site).
When you want to clear your screen, all you have to do is call the function clear_screen() which is immensely better than creating a shell to do it.
That's all,
LEAVE A LIKE IF YOU LEARNED ANYTHING
[/code]
Always confirm via PM before dealing with me.