[no audio]
Friends, we are going to discuss about 'os' module, operating system
module, which is very, very important whenever if you're going
to automate your tasks at server side, because the 'os' module,
using this 'os' module you can work or interact with your operating system.
See whenever you are able to interact with your operating system,
you can automate some tasks like creating directory, removing,
changing your directory, knowing your directory, right, and many more.
So, that's why this is very, very important.
And just to get confidence on your 'os' module, myself
I am dividing this module as four parts, that is simply
'os', and 'os.path()', then 'os.system()', and then 'os.walk()'
So that means I want to explain this 'os' module with step-by-step.
So in this video, we are going to see some simple
operations with your 'os' module, right?
See this is a module, right, 'os' module. That means using this 'os' module,
you can work with your Windows operating systems,
as well as Unix-like systems, right?
Let me take a very simple variable from your 'os' module.
You know module always consists of some functions and variables,
right, or simply module consists of some operations.
One of the operation is 'os.sep'.
What is this 'os.sep'?
See, if I take some Windows operating system path, right?
Let me take this path. See this '\' is called OS separator on your Windows.
Let me take your Unix systems.
So now this is the different '/', right.
This '/' is the separator for your Unix-like systems. Now,
if you want to set some
path separator you don't need to
set manually, based on your operating system Python will set it.
Suppose let me enter into my Python terminal,
and you know whenever if you want to use 'os' module,
you have to import your 'os', right, and then to see list of your 'os'
module operations simply you can run 'dir(os)'.
So, these all are the operations with your 'os', but I am
going to divide this into 4 parts, and I am going to explain that step by step.
So, you know that guys, these all are like some
of them are functions, and some of them are variables.
If you want to know which is a function,
and which is a variable, you have to go with 'help'.
If you press 'Enter', right, you are going to get your
'os' module information with functions, and then some data, right.
So if you want to see you can go through that.
I'm not going with that.
Let me simply print on your Windows operating system,
'os.separator'. You're getting '/'. Same way, let me
go with your Unix-like systems.
I am going to enter into my Python terminal on my Unix system,
that is Linux system.
Let me 'import os' first. Then I am printing 'os.separator'.
Right. Automatically based on your operating system, Python is finding.
Now, this is the different '/', right?
Fine. Now, let us assume that, let us assume that
I have some simple directory called,
let me take on my Desktop. Okay, I'm going to
create some directory, new directory
[no audio]
like 'hi'. Then I'm going to create one more directory, one more directory.
Let me create it.
Simply I am going to create like, I will take 'New'.
[no audio]
Fine. Now, let me take this path.
So 'path' is always a string guys.
If you go anywhere, right, 'path' is always a string.
Let me take 'path' is this one.
I am storing into some variable.
First I will take 'hi'.
This is my path.
Right. Okay, you're getting some error.
Then let me take 'New' also.
See why, whenever if you take a path your Python is giving some error
but if I take on your Unix-like systems, right,
let me take 'path=
[no audio]
/home/ec2-user'.
[no audio]
Right. Now, it's taking perfectly. The same thing,
let me take different path. Instead of this one
let me take some 'f', then in that let me take some
'\python\videos', something like that.
Now, it's taking. There is no error. Why you are getting an error
whenever if you're assigning a path to some variable?
Not only variable, even if you try to print your path you will get an error.
[no audio]
See that.
[no audio]
The reason is, guys you know in your Python you have some
special characters like '/n', '/t', '/r', right, '/v', and then '/u', is unicode.
Right. That's why to avoid that special case purpose
you have to provide '//' in your Python.
Now see that. There is no problem by taking in this way.
[no audio]
So, the problem is with your only Windows operating
system not with your Unix-like systems, because in Unix-like
systems the path separator is this one, but Windows this is
the path separator and you know '\n' is a special character,
'\r' is a special character.
'\u' is some unique code, that's why, that's why whenever
if you are providing a path
on your Windows operating systems always, whether it is having
some special characters or not, try to provide instead of '\',
just try to provide '\\'. By providing '\\'
there is no problem with your Windows,
but if you don't provide, sometimes you will get an error.
If you don't provide '\\' in your path, sometimes there is a chance to get an error.
That's why just to avoid error, even by mistake to avoid your error
try to take always '\\' in your path. That is the good practice.
Just remember this point.
That's fine.
Now, what is the next operation?
'os.getcwd()'. Guys to know your current working directory
on Windows you have to run 'cd' command, on your Unix
you have to run 'pwd' command. But instead of running this
one and this one based on your operating system
just go and run this. Irrespective of your operating system Python
will take care and it will give the current working directory.
See that. I am going to take first Windows.
Let me print. Let me print 'os.getcwd()'.
See that, you're getting some path.
Let me go and run it on your Unix as well. 'os.getcwd()'.
I can also assign this output into some variable.
Sorry, I have given some extra parentheses.
[no audio]
Right. Or let me take current working directory variable, 'cwd'.
I can assign 'os.getcwd()',
and then I can print that 'cwd'. No problem. That's it.
See now you don't need to worry about your operating system,
okay. Generally to get your current working directory on Windows
you have to run 'cd' command, on your Unix-like systems
you have to run 'pwd' command, but if you go with Python
you don't worry about which operating system you are using to
get your current directory. Python will take care of that.
The only thing you have to remember, 'os.getcwd()'. That's it.
Then 'os.chdir()'. Let me do one thing. As of now
you are under this location, 'C:\Users\Automation'.
Now, I want to move into different location. Then simply run, in your
script also you can use this, 'os.chdir()'. Guys, you have to
provide 'path', to which location you want to move.
'path' is always a string, and remember that you are working on
Windows, you have to provide two '\\' in your 'path'.
[no audio]
Okay, already we are providing same path, right.
Let me remove this 'Automation'.
[no audio]
Previously you are under this location.
Now, I am moving into this location with the help of this syntax. Now see that.
What is the current location where you are, using 'getcwd()'.
That's it.
Same thing you can do it on your Unix as well. Right. Let me do it
at least this one on your Unix-like systems.
So, I want to move, 'os.chdir()', just I want to move into 'home' location.
Now, see that, where you are.
That's it. So guys, is make sure that while working with Unix-like
systems don't provide any double slashes in your path,
but in Windows you have to provide it.
Right. Fine.
See, again to list some files on Windows
you have to use 'dir' command, on Linux you have to use 'ls' command,
but instead of that simply you can use 'os.listdir()'.
Right. Let me run it on my Linux at least. Same you can run it
on your Windows as well. 'print
(os.listdir())'. See the output.
[no audio]
Right. Otherwise, I will do one thing, 'os.chdir()'.
I want to move into '/home/ec2-user'. So in this location
I want to list. So you can move and then you can list or
directly you can do one thing. Let me do it.
'os.', 'print(os.listdir())'.
You can also provide path, for which location you need to list out your values.
I mean files and directories using in this way.
[no audio]
I think I need to provide one more parentheses. Fine. See that.
See, 'os.listdir()' is going to take a path, or if you don't provide a path
it will take your current location as a path, and it will list in the
current location whatever the files and directories
are there. And output is always a list make sure of that.
Output is always a list.
You have to remember this, because sometimes
you need to use this output. Right. Fine.
So 'os.listdir()' without path, with path.
Then 'os.mkdir()'. See you can use this just
to create your directory maybe on Windows or Unix.
You can practice with your Windows as well guys.
I am doing as of now on Linux, but you can do same thing on
Windows as well. See as of now in the current location first, let me check
what are the files and directories are there with the help of 'os.listdir()'.
So current location, no need to provide path.
Now, what I am doing is 'os.mkdir()'
I'm going to create suppose "narendra" directory.
What happened? Maybe,
[no audio]
'home'. We are under 'home' location, right. 'home',
I don't have permission for that.
Let me move into my 'home', 'ec2-user' home. 'os.chdir()'.
[no audio]
Then '/home'. '/home', I don't have permissions in that location,
that's why I have to enter into my location, 'ec2-user'.
Then here I can make directory called, suppose anything, 'narendra'.
[no audio]
Right. Now
'os.listdir()'.
See previously we ran 'os.listdir()', this location. See the list of files
and directories you have. Now see that you have
somewhere 'narendra'. Previously you were not having this. 'udemy'
and 'June-2019', but between 'June-2019' you have a 'narendra',
because just now we created that with the help of 'os.mkdir()'.
Right. Fine.
[no audio]
And make sure that guys, whenever if you are providing
a directory name that is also a string, you have to write inside of quotation
Fine. The next one is 'os.mkdirs()', and then 'path'.
That means if it is in the current location, you can use 'os.mkdir(),
but recursively if you want to create,
I mean suppose assume that you are in somewhere
'/home/ec2-user'. In this path you are. Now,
I want to create some 'xyz'. In that 'xyz' I want to create 'x'. So at a time
I want to create xyz', and then in that 'x'. Recursively I want to create this path.
Then at that time you have to take 'os.mkdirs()'. See that.
If I take simply 'os.mkdir()',
suppose I want to create under 'udemy'
I want to create 'xyz' and then 'x' path.
[no audio]
Right. It's not possible. But if I take 'os.mk', this one, right,
'os.mkdirs()'. Now, let's observe that. Under 'udemy'
I want to create 'xyz', and then in that 'x' I want to create.
[no audio]
Right. Now just list, 'os.listdir(udemy).
[no audio]
See the result.
[no audio]
Right. That's it.
Otherwise come out here, 'ls udemy'. Now recursively
you can see I think using 'tree' command, see that,
sorry, maybe 'tree' is not there.
Let me install it. 'sudo yum install tree' command.
[no audio]
So that you can see recursively. 'tree udemy'. See that inside 'udemy'
you created 'xyz', inside that 'x'.
So, recursively if you want to create you have to use
'os.mkdirs()'.
Now to remove some path you can use
'os.remove', right. And then recursively if you want to remove
I mean just now we created recursively, right. Under
'udemy', 'xyz', under that 'x'. Now if you want to create 'x', or recursively
this 'xyz' under 'udemy', then you have to use 'os.removedir()',
then your path. Path is always a string. Then you can also create
some directory using simply 'os.rmdir()'.
So guys, these are some of the simple 'os' operations. Along with that
if you want to rename some file name, right, 'os.rename'
source location, destination location.
Both must be a string.
You just try it.
Then 'os.environ()', which is very, very important guys. See
let me run it on my command line, 'env' command.
You're getting some environment variable values
of your operating system, right.
Now same things you can get it with your Python as well,
I mean your Windows or Linux operating system environment
variables you can get it with the help of 'os.getenviron'.
See the output.
[no audio]
Oh man, 'get'
[no audio]
Simply, sorry. 'os.environ()'. Yeah. 'os.', ;e-n-v-i', 'environ'.
[no audio]
What happened? Let me check it.
Where we are?
[no audio]
A, B, C, D, E, E
[no audio]
Oh, sorry, 'os.'
Yeah, this is a variable, right?
[no audio]
'environ', See the output.
You are getting some environment variable information.
[no audio]
Right. So, simply 'print(os.
[no audio]
'e-n-v-i-r-o-n', 'environ'. That's it.
So, you are able to get your environment variable values
of your operating system using 'os.environ()'.
Right. Then fine.
What is the user, 'uid'? You know, you have a 'uid' for your
each and every user on your operating systems, right?
Let me run it. 'uid' of this. Sorry, 'id'.
We're going to get 'uid' of your user 500, right, and 'gid' 500 as of now, right?
See, let me get it
from my Python. 'import os'. 'os.getuid()', User Id - 500.
'os.getguid', Group Uid.
What is that?
'getuid()'.
Let me see the operation so that it will be clear for me.
'getuid()'. 'get'
Where is 'getuid()'? 'getuid()'
[no audio]
'process id' you can get it.
Right. I want to get 'group id'.
'get groups', yeah. 'getgid'. So what I did?
I did I think 'getguid()',
no. 'os.getgid', group id.
That's it.
Right.
Then 'pid', process id. 'os.getpid',
your current shell process id. Right.
Yeah, so guys these are some simple basic 'os' operations, right?
So irrespective of your operating systems if you want to
get some details of your operating systems,
if you want to do some tasks like creating directory,
removing directory, changing your path, right, or getting your current
working directory irrespective of your operating system.
See without your Python, if you want to run suppose change directory
commands are different based on your operating system.
But if you go with Python, no need to worry about your operating
systems, just use these syntaxes so that your Python
will take care based on your operating systems.
And this is guys, this is a variable don't use this parentheses here.
'os.environ'. Right. Fine.
Okay guys, thank you for watching this video.
[no audio]