[no audio]
Friends here we are going to discuss about 'os.path' module.
Actually, this 'os.path' is a sub-module of 'os'. Let me open my
command line. I'm going to open my Linux terminal. So here let
me enter into my Python 3 terminal, right. So here I am going
to 'import os'. Now you know, you can see the list of operations
with your 'os'. Now if you see, somewhere you have 'os.path', right.
Let me go and see that. 'os.path'. This one, right. This is the
sub-module of your 'os'. So I want to see what are the operations
are there with 'os.path'. So you can do it once again
in this way, 'os' directory are operations on your 'os.path'.
See that. You're getting some of the operations from your
'os.path', right. And this 'os.path' sub-module is used
to work on path, like you know whether particular path is there
or not, whether that path is the directory or file path, right,
what is the size of that, when it was created,
what is the access time,
what is the modified time, right, whether it is a soft link
or not. So in this way to work with your path we are using
'os.path' module, right. Let me go with some of them. First thing
'os.path.sep'. We know 'os.sep'
is also having your OS separator information. The same
way 'os.path.sep' is also your operating system
separator, right. Let me do one thing. Simply print 'os.path.sep'.
You are getting your separator of your operating
system, right. Then next one, 'os.path.basename(path)'
First of all let me do one thing guys.
[no audio]
Suppose I'm going to take
[no audio]
a path called '/home/ec2-user',
and then some, okay, let me simply copy this path, and I'm going
to enter into my Python terminal, or you can also open some
script, no problem.
Let me do it, this thing otherwise 'vim os_path.py'. I am going
to import first 'os' module, right, then I'm going to take a
path in this way guys. I don't want to again and again type
this path, that's why I'm storing my path into a variable called 'path'.
Now what I want to do is, see guys 'basename' means, right, see
in this entire path, let me show you that first.
'print(os.path.basename(path))'.
See the output what you are getting.
Right. Let me save this and run this with your Python 3.
See the output, right. See, in your given path, basename means
this is, what is your exact last ending value in your path?
It may be directory or path, right. You have one more thing,
that is, let me print now 'os.path.dirname(path)',
that means whatever the file or path, complete path you
are providing, in that path
what is the directory name?
I mean, first let me run this. Before running this
let me open your script once and then I'm going to run.
So actually you are running a script to know what is the
directory name of your path.
See guys, if you take this is the path.
This is called directory name for this file.
Right. See the output. You're getting that one
only. 'basename' means this part
you are getting, directory name means you are getting this
path. So this is very, very helpful whenever
if you are installing some packages.
[no audio]
Right. Fine.
Then what is the next one, 'os.path.join'.
So this is used to join two paths.
Suppose guys, just assume that you don't have this 'os.path.join' operation.
Now what I want to do is, I want to add two paths.
Guys, let me enter into my Python terminal.
So before going to enter.
Let me do
this. Now I'm going to enter into Python.
Let's say I have two paths.
Path one is suppose '/home', then path two
I'm having some 'ec2-user'.
[no audio]
Now I want to add these two paths.
You know these two are strings, you can add it.
Like if you remember you can add two strings in this way.
String one plus string two.
Right. See the result?
Yes you are getting. But these two are the paths. Between these
two paths you should have path separators as well.
That means manually
you need to add your path
separator. 'path1 +', your path separator of your operating
system, now I'm working with Unix operating system
that's why I need to do,
I need to provide this '/', if it is Windows
other '\' you have to provide, right, then 'path2'.
In this way you can get it.
[no audio]
But whenever if you are writing a script
in that you are doing,
you are adding some two paths.
But based on operating system you need to provide slash.
But if you take this one, I mean
let me take import 'os'.
Let me print now 'os.path.join',
just I am providing 'path1' and 'path2'.
I am not giving the separator to add these two paths.
Python will take care based on your operating system.
See that I'm not giving this '/' no. By default
your Python is providing that '/'.
If it is Windows, it will provide different slash.
If it is Unix it is providing this one.
So automatically your Python will add by providing your path
separator between two paths.
So it is very, very important guys, adding two paths. See,
two paths are strings.
You can also add your strings in this way.
But whenever if you are doing in this way based on the operating
system you have to provide your slash.
But if you take 'os.path.join' Python will take care
about your separator.
That's it. Simple no.
Then 'os.path.split'.
See whatever the path you are having in your hand, right,
if you split that, it is going to split the path name into
a pair which consists of head and tail.
I mean, let's say somewhere we have path, right?
Let me take this path. Assume that this is a path. This is
a path. So in this path this is called head, and this is called
tail. See if I do simply 'os', let me print the result directly,
and guys you can also assign this operation's output into
some variable. 'os.path.split()', your path,
whatever the path you are having in your hand.
See the result. You are getting a tuple which consists of
head and tail. Now this operation output you can assign you know, this
is you know 'path.dirname' output. This is 'basename' command output.
You have done this, right. 'basename' and then 'dirname', that's
what it is giving. 'split' operation is going to give 'dirname',
and then 'basename' outputs. Fine. Now, just get the path size,
I mean whatever the file you have in your hand,
I want to get the size for that. See 'os.path.getsize()' is
the operation. See, nothing is there. You don't worry. Where
these operations are there? 'dir(os.path)'
It will give a list of operations. From that I'm taking some
of them for time being.
And by default this operation will
give your result in terms of bytes.
Right. See what I'm saying is, don't need to remember all these operations
just have a glance once here. So from this I'm taking some of them.
[no audio]
Right. Sorry, not 'os.sep', right, 'os.path'.
[no audio]
That's it.
Fine. Now, next one is, 'os.path.exists()'.
See guys, just assume that you are going to start some application,
to start some application
you have some command for that application on your operating
system. So to start that application I need to run some particular
file, but before going to run make sure that whether it is
there or not. If it is not there you can't do anything, right.
That's why just for 'if' condition purpose, right, you can use
this one, 'os.path.exists()', your path. If really your
path is existing, guys our path is, I stored somewhere 'path'
as this one. If 'test.py' is there, then only I can run it. Yes,
you are getting 'True'. See what I am saying is, I can write
a simple Python script. In this I am taking in this way.
'if os.path.exists(path)', your given path, then I can say, 'print(
"your file is there")'.
[no audio]
Right. See let me run it. 'else', else if that is 'False' then I can say
that print this, let me print it.
[no audio]
"is not present
[no audio]
on this host". That's it.
Right. Let me run it once.
[no audio]
'python3', script. Yes, you are getting, "your file
is there". What I'm doing is, I'm just going to modify my path
value, something like that, 'ec2-user' is there
no, I'm removing some 'e' letter.
Now, that is not a valid path on my host.
Now if I run that, see, "is not present on this host". Yes, it is
not present. So to take some decision based on your file
with 'if' condition this is also helpful.
Right. Then, see given path maybe file path or directory path.
You can check it. 'os.path.isfile()'. In case whatever the path
you are providing,
if that is the file path then this value will give output as 'True'.
Then if it is a directory path then 'os.path.isdir()'
will give that output as a directory.
Now see that, what I am doing is, I want to decide.
I want to decide whether the given path is a file or not.
Then what I will do?
Let me comment all these lines.
Right. I'm going to comment all these lines.
Now, I'm implementing a simple script that is,
'if os.path.isfile()',
whatever the path you are having in your hand, then I can
say that, 'print(
[no audio]
"is a file")'.
That's it. 'else',
if it is not a file we have only one other option, that is
'print("
[no audio]
'print("your path is a directory")'.
[no audio]
That's it.
So I can use only 'isfile',
and I can decide.
Because if it is a file, then 'os.path.isfile' will become true.
Then you are going to display this line.
Right. See the output. Yes.
Now what I am doing is, I'm going to modify your path.
I'm going to give up to directory.
[no audio]
Right. Now, see the result what you're getting.
"is a directory", you are getting.
Yes, it's a directory. Previously you got file,
now it is a directory. See now, to check that I can use either
'isfile' operation or 'isdir' operation in your 'if' conditions.
Then, 'os.path.islink()'.
See, you know sometimes we have some soft links, right.
Let me check it,
is there any soft link?
[no audio]
No. Let me do one thing.
I'm going to create a soft link,
'os.path'
I'm going to create 'myos', something like that.
Now see that. Now 'myos' is a soft link, right?
I want to check that with Python, whether it's a soft link or not.
Simply import your 'os'.
You can also write your script. I'm doing from command line.
Right. See. 'print
(os.path.islink)',
then just provide your
suppose, 'myos' file, or you can also provide complete path.
As of now, I am in the same location
that's why I'm not providing complete path. Yes, true.
But what I am doing is, 'os.path.islink',
I am providing suppose
'os_path.py'. This is not a
softlink. This is simply a file, right?
That's why we're getting output as 'False'.
So based on this True and False I can tell that this is a
soft link and this is not a soft link by using 'if' condition.
Right. So, guys, these all are some of the
operations from your 'os.path', and which are very, very helpful
in your real time. Along with that you also need access time,
right, some creation time, modified time.
So these times you
can get from your 'os.path'. 'os.path.getatime'.
But to understand these three operations you should be good
with some time module of your Python. So after discussing
time module once again we'll come back to your 'os.path',
and I will explain about these three operations.
Right. Okay.
Okay guys, thank you for watching this video.
[no audio]