[no audio]
Friends, here we are going to do
simple practice with your platform and
'os' modules. Task is, write a single Python script to clear
terminal of any operating system or I can say write a platform
independent script to clear terminal.
See here,
my intention is I want to write one Python script, and that
Python script has to run on any of your operating system,
and while running that script on your any one of your
operating systems
it has to clear the terminal. That means first
let me open my Windows operating system. Suppose
I am running in this way. Now clear screen means, suppose if
I run here Python script it has to do in this way,
it has to clear my terminal. Same way suppose
I am, this is my Unix or Linux system, right, if I run your
Python script here,
it has to clear a screen, it has to do in this way.
So in that way I need to implement my script.
See guys, by default to clear your terminal suppose as of
now based on our previous concept, suppose
we don't have any, we didn't see any command in Python directly,
but we know one thing, from 'os' module we can run any operating
system command, right. That's why
what I am doing is, I am writing a simple Python script, right,
that is 'clear_screen'.
Guys, directly you can run 'clear' command on your Unix-like
systems, and the 'cls' command on your Windows operating system,
but to provide some idea to use your different modules, to
work with your different modules
I am taking this simple example, right, 'clear_screen.py'.
See I can import, I can import 'os' module, right?
See if it is Unix-like system,
I have to run 'os.system("clear")' command,
and if it is Windows I have to run 'cls' command. See my intention
is, I want to write one Python script, and it has to work with
all operating systems. Suppose if I take only 'os.system()',
and then 'clear',
right, then it will work with only your Unix-like systems.
Let me run this 'python3', or first of all randomly, right.
[no audio]
Now, I am going to run your script. 'python3', then 'clear_screen'.
Yes, it's clearing your screen. But same script
I want to copy,
[no audio]
to your Windows operating system. There
I want to run. Let me do it one thing.
'vim clear_screen.py'. So here
I am going to copy whatever the code you are having here.
Let me copy that code.
So this code I am copying, and I'm going to paste it on
your windows.
[no audio]
Right. Now, see that. Let me save it and let me run it.
'python', on Windows you know 'python' is there,
'clear_screen',
See you are getting an error, 'clear' is not recognized as
an internal or external command, because 'clear' command
is for your Unix-like systems, not for Windows.
Now what I have to do? Whenever if you're running from Windows,
right, I need to run 'cls' instead of 'clear',
right, but because of this modification you have two different
scripts. See this is 'os.system("cls")', but this is 'os.system("clear")'.
But what I have to do is, I have to run same script on
Windows as well as your other Unix-like systems so that it
has to work. Then see that, first of all I am writing,
Sorry.
[no audio]
I am writing both the commands here.
'os.system(
[no audio]
"clear")', and then 'os.system("cls")', but I want to
run this command if it is Unix-like systems,
I want to run this command if it is Windows operating system,
right. Suppose first
I will take 'cls',
then second I will take 'clear'.
See guys, if you remember
you can identify your operating system name from this module,
'platform' module, right?
See what I am doing is, 'if platform.',
'windows', sorry, 'system',
[no audio]
if it is '== "Windows"', then I am going to run
'os.system("cls")', 'else'
I am running 'os.system("clear")'
Right. See, how it is going to work? You know 'platform.system'
is going to give your operating system name on which
you are running your Python script. Suppose
if I run this script on my Windows operating system
then this value becomes 'Windows'. If it is 'True', you know
your python 'if' condition is allowed to execute this line,
and you know in 'if-else', if 'if' condition is 'True', then by default
'else' is going to skip. Same skip just assume that you are
going to run on your Unix-like systems. Whenever if you are
running this script on Unix-like systems,
this value is your 'Linux', then this is 'False', right?
If it is 'False', then 'if' doesn't allow to execute this line,
and if this is 'False' by default without checking any condition
'else' block will execute, right. Now,
I am copying this code.
First of all, let me run this on your Windows operating system.
[no audio]
Yes, it's prompting.
'sys', yes. Spelling mistake guys.
It's my bad.
[no audio]
's-y-s-t-e-m'
[no audio]
Yeah, it's correct here.
[no audio]
Let me run your script.
[no audio]
Right. Just clearing your screen.
Same code, whatever you have here, that code
I'm going to copy it and paste it on your Unix-like systems.
Right. See let me open, before that
let me nullify your previous data, and I am going to open
your 'clear_screen' script, and I am simply copying and pasting
your code from your Windows operating system.
Now, I am going to run this.
It's clearing.
Right. See guys, be clear.
So here I use the concept of your 'os' and 'platform' modules
along with your 'if-else' concept.
So now this is a platform independent script. This script
you can use on any operating system such that it is going
to clear your terminal.
So one script it is going to work with all operating systems.
Right. So this is the simple script to clear your terminal, and
it is a platform independent script.
So that is important guys here.
So whenever you are writing Python script, right, and in your
real time if you have multiple operating systems, then you
have to write your Python script in such a way that it has to
work with all your operating systems.
Right. Okay.
Thank you for watching this video.
[no audio]