[no audio]
Friends, here we are going to discuss
about 'os.system()' of your 'os'
module. Actually,
this 'os.system()' is a function because if you observe
here, I have given parentheses.
[no audio]
The purpose of this 'os.system()'
function from your 'os' module is just to execute
your operating system commands.
See as of now, we have seen some of the commands from your
Python to work with your operating systems,
like 'os.listdir()', 'removedir()', 'mkdir()',
'chdir()', 'getcwd()',
right? But in case if you don't have any command from your
Python to work with your operating system,
and if you know the command of your operating system, and
if you want to execute your operating system command with
Python, then this 'os.system()' is helpful.
See there is a command called 'pwd', suppose on your Unix-like
systems. What is the output you're getting? Current working
directory or where you are on your terminal, right. See,
with your 'os' module, right,
'os.',
'getcwd' is there, but assume that
you forgot about this 'os.getcwd', because if you go
with 'os.', suppose 'listdir()', there are different
commands, right, from your Python to work with your operating
system. But by mistake,
[no audio]
otherwise, simply assume that I forgot
about this 'os.getcwd()',
'os.listdir()', 'os.makedirs()', something like that.
Then what I can do is, simply I can use 'os.system()',
[no audio]
and then I can run my particular operating system commands. See that.
[no audio]
And then 'os.system()',
I can run 'ls' command. See the output.
[no audio]
Right. You're able to execute your operating system commands
with the help of 'os.system()'.
Not only Linux,
you can also execute from your Windows as well.
Let me enter into my Python.
Let me 'import os'. 'os.system()'.
[no audio]
I can run something like 'dir' command.
Yes, you are getting output.
Right. 'dir' is the command for
your Windows operating system to list your directories and
files in the current location.
Same thing you can run with your 'os.system()' as well through
Python. So simply 'os.system()' is helpful to execute
your operating system commands.
See, to clear my terminal, right,
if it is Linux I'm using 'clear' command, if it is Windows
I have to use 'cls', right. See that what I am doing,
'os.system()'. Because I am on Linux as of now
I am running 'os.system("clear")'.
Now, your terminal is clearing.
Let me open your Windows terminal. Now,
I am in Python shell but I want to execute Windows command
called 'cls'. Yes, you can execute it.
You know, what is the result of 'cls'?
It will simply clear your screen.
Yes, that's what it is doing.
But if you observe here guys, while running your commands,
it may be any command, right, see the output. You are getting,
you are able to display your output directly with the help
of your 'os.system()', right, at the same time you are getting
some return code.
So here you are getting 0, that means whatever the command
you are running with 'os.system()',
that is success.
Right. Not only there, here also
let me simply run 'os.system()'.
Now, I am on Unix box.
I am running 'pwd' command. See the result. You are getting path,
current working directory, and also you're getting some 0. That is
the return code from your 'os.system()'.
If it is 0 then whatever the command you are running, that
was success, that was successfully executed by your 'os.system()'.
Let's say I am running 'os.system()', something called 'random'
command, which is not a valid command
as per your operating system,
that's why, see the output what you are getting. You are
getting some error, along with that return code as non-zero.
See that. I am writing something like that, 'rt = 'os.system()',
some 'ls'. Guys if I run in this way, be clear, it's very important,
whatever the output you are going to get from your 'os.system()'
with the help of, to run your 'ls' command, that
output won't go and store into this. Simply the return status
of this executed syntax, that will store into this 'rt'. That
means, in case if it is executing successfully then 0 will
store into this, otherwise non-zero will store into that. Now,
see the output. So previously whenever if you're running your
command you are getting output and also some return code,
but now I am assigning that return code into some variable.
Now you're getting only output.
[no audio]
Make sure that 'os.system()'
output you're not able to store into any variable.
You can't store. The only thing, you can directly display your
output. But what it will store? In case if you assign your
'os.system()' some command output into a variable,
what is the value you are going to store into that? Simply,
simply the executed command status, whether
it is success or fail, that status you're going to store into
'rt'. Right. See that. Let me write a simple script
so that you can understand.
'vi run_os_cmds'. I am writing 'run_os.py', or
'os_cmds' instead of this. Just for clarity
I'm writing 'run_os_cmds.py', or any command also.
So I can run with the help of 'os',
I mean 'os.system()'.
You're also having different procedures guys,
we will see while going forward. For time being
it is important, I am working with this.
See, command is always a string.
You have to remember that. Suppose my command is, let's say
date command.
Right. Now what I am doing is, 'os.system()', then your command.
So already I stored my command into 'cmd' variable as a string,
now I can provide that.
Let me store it and see your script once. Now,
I am going to run this script. You're getting output.
[no audio]
Right. Whenever if you're running your 'os.system()' from
your Python script, no need to print the result. Automatically
it is printing the result.
Right. Now what I am doing is, maybe as of now date command is perfect
that's why we are getting output, right?
Maybe I am taking some wrong command. Now see the output.
[no audio]
Now whenever if it is wrong command your 'os.system()'
will give some return codes,
I want to capture that.
Let me take rt = 'os.system()', some command. Now,
I can take some 'if' condition, 'if rt == 0', then I can say that,
'print()',
"your", whatever the command you're having, "your cmd was
success", "successfully executed".
[no audio]
'else',
[no audio]
'else' because if 'rt' is not equal to 0 means fail, "your
command was fail".
[no audio]
That's it. Very simple no.
I can capture the return status of this command means if
this command was success then I can capture
0 into this 'rt' variable, you can take any variable
here, not only 'rt'. Simply I'm taking return code,
that's why I am taking 'rt' as the variable, but you can
take simply 'x' also.
Right. Now see the output.
I'm running your script. Before running
just open your script, right.
See this is your script, and now I am running that. See the
result, "your command was failed".
[no audio]
Right. Now see that. What I am doing is,
[no audio]
"Your command", right?
Yeah, I'm going to give now a valid command. Date is
a valid command, right?
[no audio]
Now see that. "your cmd was successfully executed".
So guys make sure that 'os.system()' is used to execute your
any operating system commands and while executing your commands
with 'os.system()' in your Python script by default
it will print the output of your command on your terminal
while running your script.
At the same time it is also going to give
some return code based on whether your command is success or not.
If your command was success 'os.system()' will
give 0 as the code, if your command was failed then your
'os.system()' command will give return code or code
as non-zero value. Based on that I can decide whether it is
success or not.
[no audio]
Right. Okay.
Okay guys, so while going forward we will use this 'os.system()'
command, and we will write lot of scripts using this
'os.system()', right?
Okay, thank you for watching this video.
[no audio]