Now, the next task that I'm actually going to do over here is that let's go ahead and create a new
directory.
Okay.
So create a new directory.
I'll be doing this.
Uh, let's say I will again you need to import OS.
So let's say my new directory is something like this okay.
Let's say this is my new directory called as package.
Okay I want to create a package folder, uh, over here in the same working location that I'm actually
writing a package folder over here in the file handling folder itself inside this.
So all I have to do is that I'll just go ahead and write OS dot make dir, and I'm just going to give
my new directory information.
And let me just go ahead and print this.
I'll use an F string and say that directory.
Uh and here I'm going to specifically use this new directory name is created okay.
So this will be my message.
So here you can see directory package created okay.
And here you can probably see I will be able to get this package folder okay.
And this is how you can create a new directory with the help of OS.
Okay, let's say that I want to list all the files from a directory itself.
Right?
So next task will be that listing files and directories.
I want to list all the files, and I want to list all the folders that I have inside a folder itself
where I'm actually working.
So I will just go ahead and write items is equal to OS dot list dir.
And here I'm just going to use this dot okay.
Dot basically means we are specifically looking at the current working location, whichever folder it
is inside, and it will just list and it will give you the entire information.
Okay.
So let us go ahead and display this entire item.
So if I go ahead and print items over here.
So here you'll be able to see I have this particular file this particular file destination txt example
dot bin package.
So all the folders inside this particular folder it is being able to give me okay.
Pretty much amazing.
And definitely we use this kind of file a lot because I may have a scenario that in my project I need
to probably read all the files and folders, and I need to probably do some kind of operations and I
can actually do that.
Okay.
Now talking about one more very important concept that is nothing but joining paths.
Okay.
And this is the most important feature of using this OS package, you know, because this path will
be different.
Different when terms of coding and windows separately coding.
We can just hard code it right in Linux or in Mac.
So by using OS dot path we can specifically use this.
Okay.
Let's say that I want to probably create I have a directory name which is called as folder okay.
And then let's say I have my file underscore name okay.
And this will be my file dot txt.
Now how do I create this particular path.
That basically means inside my folder there is a file dot txt right.
If I want to probably get the full path or relative path.
Also, I can say I can just write OS dot path okay dot join, Then I can actually use this directory
name.
Okay.
Oops.
Just a second.
I can directly use this directory underscore name comma.
I will go ahead and use file underscore name.
Okay.
So this basically becomes my full path.
So now if I go ahead and print my full path you will be able to see hey this is my path that I'm actually
getting right.
Let's say I want to completely get it along with my folder location.
Then I can copy this entire thing again.
Okay, I can paste it over here and here I can also do OS dot get cw d.
Current working directory along with my directory name.
Okay, so if I go ahead and write this now here you can see this is my entire path right.
I can also do this if I want to just have the relative path I can just use this two variables.
Okay.
And this is how things are amazing over here.
And you'll be able to see it.
Now in our previous tutorials we have seen right whenever we are trying to open a file, first of all
we need to create that file, right?
But here, uh, we will try to manipulate, you know, or let me just show you one example.
Okay, so here what I'm actually going to do I'm going to create an example file.
So let's say this is my example one dot txt I will say hey if os dot path dot exist okay.
If this particular path exists or this particular folder exist, then I will just go ahead and write
print f okay, print f.
Here you can actually see the path the path this specific path exist okay.
That basically means that folder also exist.
Else I will just go ahead and print f the path.
Path does not exist.
Okay.
So here if I go ahead and execute it Here you can see that the path example one dot txt file does not
exist because I don't have that specific file.
Now let's say if this file is not there what I can do I can.
If it is not there or if this file does not exist, I will go ahead and create that particular file
with again you with open command or with any kind of command, I can go ahead and do it right.
So this is very much important.
And this kind of condition needs to be used in our coding environment whenever we are making some assumptions
of a specific path itself.
Okay.
Now this is fine.
There is also one more way to check whether the file is present or not.
Okay.
Um, so here I will show you.
So I will just go ahead and write this comment checking if the path is a file or directory.
So there is one more method over here.
So here you can see import OS I'm using path is equal to example dot txt.
Now this file right OS dot path dot is file of path.
So this file it is going to check whether this path is a file.
If it is a file then it is going to say it is a file if this path is a directory.
So for that I have is directory function.
Then it will go and say that hey, this path is a directory.
Else this path is neither a file or directory.
So if I go ahead and execute it, I will go ahead and see this.
Right.
So here it shows the path example dot txt is a file okay.
So this is how things are basically working over here.
And you will be able to see that.
Hey yeah this particular file is a file because example dot txt is a file.
Okay.
Uh as I already told you okay.
Relative path is something different.
Relative path basically means this file inside this particular folder file dot txt.
If I'm seeing this becomes a relative path okay.
Uh and the absolute path is the complete path okay.
Let's say for any folder location I want to get the absolute path okay.
Getting the absolute path.
Because I may be requiring some or the other thing over here.
Okay.
So here what I will do.
Let's say that this is my relative path.
So this file is basically present inside this folder which is called as example dot txt.
Now I will go ahead and create my absolute path.
Absolute path okay.
And for this I will go ahead and write OS dot path dot apps path.
So apps path is one method over here.
And then we can use this relative path.
Okay.
Now if I go ahead and just print my absolute path here, you'll be able to see the complete path.
I'll be able to get it right again.
Uh, if you do not want to go ahead and do in this way, you can specifically go ahead and join all
these particular things along with the directory name.
But I feel this is, again, better when compared to that one.
So I hope, guys, you were able to understand all this file path operations specifically with the help
of OS.
Again, there will be multiple examples which we'll be seeing, but most of the common operations that
we usually do in the code, uh, is probably discussed over here.
This will be the most useful operation or most useful library when you are working in an end to end
projects, working with different different files, working with different different directories over
there.
Uh, so there we are going to extensively use it.
Right.
So please make sure that you practice this more see different different examples with respect to this.
And yeah this was it from my side.
I will see you all in the next video.
Thank you.
Take care.