[no audio]
Friends, here we are going to
write one simple Python Script. That is,
read a path and check if given path is a file or a directory.
So you have to write one Python Script such that it has to
read some path. After providing path,
right, your python has to validate that, whether the given path is
for file or is for directory, right. Fine.
Let me open my editor first.
So guys, we know if you want to read a path, path is always
a string you need to remember that. Path is always a string.
If you go with any operating system path is always a string.
First let me save program as 'identify_given_path',
or 'working_on_path'.
[no audio]
Let me take something like 'path_file_directory.py'.
Fine. See my intention is, I want to read a path first. So path is
always a string.
That's why you know in your Python
you have a 'input' syntax to read any string. Now I am taking
variable as 'path', and I'm asking the user "Enter your path: ".
[no audio]
Right. So after that what I have to do?
Whatever the path you are providing that path
I have to check whether it is a file or directory path. Then
how can I validate that? See, previously we discussed somewhere
your 'os.path' sub-module of your 'os'. 'os.path.isfile()'
'isdirectory'. You can go with anyone of that.
So now I want to use 'os' module,
that's why first you need to import your 'os',
[no audio]
then whatever the 'path' you are providing
I'm checking with 'if' condition.
'if os.path.isfile',
that means if given path is a file then this will become
'True'. If it is 'True', then whatever the logic you are going
to write under your 'if' condition that will execute. Now what
I want to display?
[no audio]
"The given
path: ", whatever the path is there.
[no audio]
Sorry, "is a file". That's it.
So in case if given path, given path is not a file, then this
condition will become 'False'.
That means that is not a file.
If that is not a file, then what is our option?
Definitely, it is a directory, right. Then
let me do it, 'else' block
I am writing, 'print("
The given path :", whatever the path you are providing that is,
"is a directory path". That's it.
Let me save it.
Now I want to run this.
So let me open my command line.
[no audio]
So already I am in the location where you have your script, this one.
Let me run it with your 'python3'.
I am working with Python 3 only guys. Sorry.
'python --'
[no audio]
Let me do simply this thing. Yeah.
Now let me run your script, that is, 'path_file_dir.py'
Yes, it is asking something.
So guys, we are working on Windows, right. See whenever you
if you are working with Windows you always, you have to always
provide your path as, I mean while entering your path you
have to provide '\\' in your path instead of '\'.
You know what is the reason. We already discussed that in
our previous sessions.
Now let me enter it and see the output. "The given path : ", "is a directory".
Yes, you are getting the thing.
Now, let me rerun and I am providing same thing,
but at the end I am giving our Python Script name itself, no problem.
You can provide anything.
[no audio]
Now see the result what you are getting, "is a file". Right. Now
let me rerun,
and I am giving something called,
some "x:
[no audio]
\\python". So this path I am giving, but output you're getting,
"is a directory".
But if you observe, this path is not there in my host. If you
go with my system, see that I have only C, E, and F, G directories.
I don't have this directory no, 'X'. 'X' Drive
I don't have but still you are getting information as "is a directory".
It's absolutely wrong. Why you are getting this situation
means, you are checking if given thing is a file,
then you are executing this.
So whether it is a directory or if it is invalid then at
that time by default this result would become 'False', at that
time you're executing 'else'.
That's why what I have to do is before checking whether that
is a file or directory,
first of all you have to check whether that given path is
existing or not.
Right. See that's why what I am doing is, let me delete your entire code.
[no audio]
Now what I am doing is, 'if os.path.exist',
if a given path is existing, then only I want to identify
whether that is a file or not.
If it is not existing I don't want to do it.
[no audio]
Right. Fine. Now I am saying simply.
"Given path is valid".
[no audio]
If it is valid then only I can identify whether it is a
file or not, 'else: print()'.
[no audio]
"Given path", so if you want to write that path you can write
it, right, "is not existing on this host".
[no audio]
That's it.
[no audio]
"Given path", whatever the path you are giving let me write
that path, "is a valid" one,
first thing, is a valid path. First
let me run this, then we will add your code.
[no audio]
Not. Let me go to command line.
Let me run it.
I'm running and I am providing, before running
let me do this thing.
[no audio]
This is your code.
Now, I'm running, and I'm going to provide some random thing, see
"x: is not existing on this host".
Actually, that is not a valid path
that's why we are getting, that is not existing on this path.
Now, let me rerun and provide some valid path.
See this is a valid path, right. Whether it is a file or not
secondary. First of all, I am checking whether that is existing
on your host or not.
That's what you're doing here.
'os.path.exists'.
See that. "Given path is valid path", even if you provide directory
also, it will work. See up to this, this is a directory path,
right. It's working. So after verifying that is a valid path,
right, after that you have to check whether that is a directory
or not. That's why inside of your 'if' condition you have to
write one more 'if' condition, that is 'if os.path.isfile,
then, then I can write 'print
("Given path : "', whatever the
path is there simply I will write, and "it is a",
[no audio]
and "it is a
file path",
'else:'. 'else:'
See first of all, after existence of your path you are entering
into this block, inside of that your 'if' condition once again,
we are verifying whether that is a file or not with your
'os.path.isfile'. 'isfile',
this will become 'true',
then you are printing, "and it is a file path".
If it is not, then what you are displaying, "and it is a directory
path". That's it.
Right. See that. I am going to run,
[no audio]
and I'm going to provide. See that. "valid path", and "it is a
directory". Right. Then rerun and provide some file path.
Yes. First of all, it "is a valid path", "and it is a file path".
That's it.
Right. So guys, this is the way
simply how you can verify given path is
a file path or directory path.
So make sure that before validating your path is a file or directory path
first of all you should check it,
you should verify it whether that given path is existing
on your host or not.
If it is not existing, then you can't identify whether it
is a file or directory.
[no audio]
Right. Fine.
So this is the way how you can work with your Python
to identify your paths,
I mean like file or directory.
Right. Okay.
Okay guys, thank you for watching this video.
[no audio]