[no audio]
Friends, here we are going to write
one more Python script. Requirement
is, read a directory path and then identify all files and
directories in the given path.
See simply our script should read some path, directory
path. So in that location, in that given path, what are all the
directories and files are there, right?
I want to display all those things by mentioning,
this is a file and this is a directory.
Let me take one simple location.
Yeah, see here.
I have to provide this as a path to my Python script. Then
my Python script has to display
'mydir' is a directory, 'New Text Document' is a file,
'xyz' is a file. Your script is able to identify, it has to display,
this is a directory, and this is a file, and this is also a file.
Right. Let me go and write one by one. For time being
what I am doing is, let me remove one file here just for simplicity
purpose. Finally we'll write it.
Let me save it.
[no audio]
'read_a_path_and_identify_files_and_dirs'.
Fine. See here.
[no audio]
Guys first thing, we need to read a path, right?
So to read a path because path is a string, in your Python
you have 'input' to read any string. "Enter your directory path: ". That's it.
[no audio]
After reading that path, first of all I want to display
all the list of files and directories in the given location.
Right. If you remember, there is an operation from your 'os'
module that is 'os.listdir'.
So now here I have a requirement with 'os' module,
that's why importing 'os'.
Now simply I am printing
'os.listdir', and whatever the path you are providing
that path I am taking.
Right. Let me go to
my command line. From there
I want to run, because I need to provide this path, right.
So our script location is in this somewhere. First
let me open your script.
This is your script, only we have three lines.
First line is, you are importing 'os' module. Through second
line you are going to read from your command line, your path.
Third line, in the given path
you're going to list out all the directories and files.
Let me do it.
[no audio]
Right, and I want to provide a path.
Let me take this as my path.
And I am providing, see guys this script will work both on your
Windows as well as Unix-like systems, but while providing path
on Windows operating system you have to provide '\\'
in the path. See the '\', we have single
'\', no, but you have to provide '\\' just to avoid whenever
if you have some special characters in a path. Right. Only
whenever if you are providing manually at that time only you have
to provide. Fine. Now see the output. This path, whatever you're
providing, that is going to store in this variable, and I am
listing 'os.path.', 'os.listdir' in your 'path'.
So in this location, I want to list all the directories and
files. And this output is a list. See that. Already we have
seen in our 'os' module explanation
this is always a list. Now you are getting two files,
one is directory and one more is file. But my requirement
is, if I get some values in the given path, I mean some files
and directories, our Python script has to display,
this is a directory and this is a file.
[no audio]
Right. So, you know, if you know the path, if you know the path of
your file, then you can find out whether this is a file or
not using your 'os.path.isfile', 'isdir'.
You can take any one of that operation if you remember.
Anyway before going to check whether it is a file or not,
first of all you should have in your hand your complete
path of your file or directory.
Otherwise, you will get an error. Because your script you
are running from this location, but these files are there
in a different location.
Right. See that. Suppose if I do simply.
[no audio]
Yeah, one more thing. Before that
let me store this output into one variable, files and directories,
files and directories list, 'df_l'. That's it.
Now, I can also print in this way, right?
[no audio]
Right. Let me rerun your code.
So this is your code.
Now, I am going to rerun that.
I am going to provide this path.
Yes, same output you are getting because of this 'print' line.
So in this variable, now you have these two things. As of
now assume that whatever the path you are going to provide,
in that path you have any two files, or any two directories, or any one
directory, and one file. Now to identify if this is a file or not,
and this is a file or not,
first of all I have to separate this value from this list, right?
So that's why what I am taking is, I am creating two paths.
Now, let me comment this line.
Path 1, let me take 'p1', path 1 '=', from your this list
index 0 I am storing into 'p1'. 'p2=', from
your this one, index 1. Right. Now I want to identify whether
'p1' is a, whether 'p1' is a file or not, file or directory. Guys
let me do it. As per your output, 'p2' is a file, right?
See because second value is a file, index 1 value.
Let me check it first on your 'p2'. 'If', you know in our previous
example also we have seen 'os.path.isfile(p2)'.
Then I can say your 'p2',
sorry, I need to provide here, 'p2', right. We're working on 'p2'. 'p2' is
a file. 'else', if that is 'False',
'os.path.isfile',
maybe 'p2' is a directory or file, you don't know. If you
know exactly you can say that that is a file,
otherwise that is a directory, right. Then I can say
your 'p2' is a directory.
Let me save it, and go to command line, and then read your
script once. Let me run your script. Now,
I am providing some path. See 'xyz.sh is a directory', you're
getting, but that is not correct.
The reason why you are getting is,
[no audio]
actually 'p2' means 'xyz.sh'.
Your Python script you're running from different location,
and your files are there in a different location.
That's why whenever if you are running any path related operation
you should get in your hand complete path. As of now
you don't have a complete path. 'xyz'
is there somewhere in different location, but I don't have
complete path for that.
Right. What is the complete path? Complete path is nothing but, in
the given location, in this given location your file is there,
then for this location at the end, at the end if you add
'xyz.sh', then that is the complete path for this file.
Right. So that you have to create first. How you can create it?
See if you remember,
'os.path.join()', your given path, your file,
so that you are going to create a complete path for your file.
See, let me print your path, only path.
I am printing only 'p2' file.
Let me comment your remaining code.
[no audio]
Now see the output what you're going to get. I am running.
You're getting complete path for your file,
no. Once if we get complete path, guys complete path is nothing
but your given path, in that path
you are having 'xyz.sh'. That's why if I add these two
things, you can get complete path. Your 'xyz.sh', and given path,
if you add those two things, you will get complete path for
your file. So that you can directly, irrespective of your operating
system to add two paths you
can use directly 'os.path.join'. That's what I did.
Not only for 'p2', 'p1'
also you have to do. Not only for that file,
if you have multiple things, for all things you have to do
in this way only. First of all, you have to create complete
path for your file.
[no audio]
Guys, why I am taking your path only, because that is the
path you are providing as the input. In that path
only your file is there. That's why if you add those
two things, you can get complete path.
[no audio]
Fine. Now, run your script. Now,
I am going to comment your two files.
[no audio]
Right. Now see that. As of now
I have a if-else condition only for 'p2' path.
Let me verify that whether 'p2' is working perfectly or not.
This is the path. Yes.
"is a file", you are getting. Same logic
I need to apply for 'p1' also, right? Then what I have to do?
Let me copy this logic once again and try to apply for 'p1'.
[no audio]
See that. I am trying to apply for 'p1'.
[no audio]
So what will happen? If this is a file, this will become 'True',
else 'False'. 'False' means, you're going to display this one.
Guys in your previous script
we also used 'os.path.exist', but here no need of that
because if a given path is valid path, then only you are going
to get list of files, 'listdir' into this 'df_l' variable.
If you provide valid path then only, right, then only you are going to get it.
If you want to write effective script here you can apply
'if os.path.exist'.
If given path is a valid path, then only I can find the list
of directories and files in the given thing.
Otherwise, I can write 'else'. 'else', 'print("please provide valid path")'
[no audio]
Right. And one more thing, anyway it's time to give that as
well. If path is invalid you are not going to get this list.
If you're not going to get this list, how can you, how can
you create 'p1', 'p2'?
You can't create. That's why if this path is not existing,
you have to print this information and you should not execute
this code. If you're executing
this code means, you got your 'df_l' variable.
Now, you are not getting. If path is invalid, you can't get this,
because this is false, right?
Without having your list, how can you use in your code?
That's why you have to stop your script. To stop your script
you have a syntax called 'sys.exit'.
Now we are using 'sys' module, so you have to import that. So
without 'sys.exit' if I run my code, see the output, how
you're going to get.
[no audio]
You will get an error. See that. Instead of this,
I am giving, "invalid path". See, if it is invalid path, you are
not getting 'df_l'.
If you are not getting 'df_l',
how can you get from that index zero?
You can't create 'p1', right. Path one
you can't create by taking index 0 from your variable.
If given path is, "please provide valid path", you are getting,
that means given path is invalid.
If it is invalid, you can't get this one.
The reason is, if it is invalid path this will become 'False'.
If it is 'False', there is no way to create your 'df_l'.
If there is no 'df_l',
how can you take 'p1', 'p2'? How can you create 'p1' and 'p2' from
this 'df_l'?
You don't have that.
That's why I need to stop my script.
[no audio]
If it is valid path, then only I need to run my script. So
if it is valid, your 'if' condition will become 'True', this value will execute,
and anyway if this is 'True', you know that 'else' is going
to skip by your Python. Now see same code
I'm running and I am providing invalid path.
Now, you are not getting any error, instead of that you are getting
simply, "please provide valid path".
Okay. It's good because here we got one more concept how
to use 'sys.exit'. That's fine.
Now, let me run this code and I am going to provide valid
path. Now see that, in that path, this is a directory.
Yes, you are getting through Python, "is a directory", and this
you're getting, "is a file".
[no audio]
Right. Fine.
[no audio]
So we are creating 'p1' and 'p2',
and we are checking one 'if-else' condition for 'p1', path 1, and
second 'if-else' condition for path 2, and here there is a
very big disadvantage.
What is that means,
as of now in the given location you have only two files.
That's why I'm taking path 1 and path 2. Assume that you
don't know how many files and directories are there in the
given directory path.
Let me do one thing.
[no audio]
I am deleting this file. Now
I am going to run your script.
Right. See the result.
[no audio]
You're getting an error, "list index out of range".
The reason is, let me print first your,
[no audio]
Because as of know, you are having only one item in the given
location. See that output.
[no audio]
Right. Only directory is there, that's why index zero. Index
one is not there. But for 'p2' you're using index 1.
Then your script is wrong, right?
Or else I will do one more thing.
[no audio]
I will create some random files here.
Let me create some files here.
[no audio]
'touch x.txt y.py z.sh', right.
Now I created three files here.
I created three files. Now for the same path once again
I'm running your Python script. Try to understand guys.
Now you have actually four
items in the path, but your script is giving about only two paths.
[no audio]
Actually you are having four values, right but
your script is saying only about two information, two items,
two paths from your given location.
Then what about remaining?
[no audio]
Why it is giving only two means, as of now in your Python
script you have taken care only for 'p1' and 'p2'.
So now you have a problem with this script. If your given
location is not having two paths, then your script is going
to give an error, or if it is having more number of files
and directories in the given location then your script is not
able to identify all. It is going to identify only two.
[no audio]
That means based on how many files and directories are
there in the given location, based on that dynamically
your script has to work. That is not happening with the script.
Then how you can do it? Then how you can do it? That's important thing.
Right. Now let me comment this code as of now whatever you have.
I will comment entire code first.
[no audio]
Be clear guys.
It's very, very useful in your real-time concept.
[no audio]
I am writing some syntax guys.
Don't worry about that.
Just try to understand the output.
I am writing 'for each in [2, 3, 4]
[no audio]
Then I'm writing 'print'. Okay.
Then 'print("hi")'.
First of all, assume that this is like 'if' condition.
You know, in case if it is 'if' condition, if your condition is
'True', then you are going to execute these two lines.
[no audio]
If it is 'if' condition, and whatever the condition you have
here, if that is 'True', then you are going to execute these two
lines once. But this is not condition,
this is called a loop.
This here, 'for', 'for' is called a loop.
What is the use of loop means,
be clear, because of this loop
whatever the values, sorry,
whatever the lines you have under your loop, those lines
will repeat some certain number of times again and again
immediately. But how many number of times you are going to,
your loop is going to repeat these two lines means, how many
values you have here that many number of times these two
lines will execute.
Right. See that. First
let me run this code and see the output. Here itself
I am running. Because I'm not providing any input
I can run it from Sublime Text itself.
You have 2, 3, 4, three values in your list, right?
That's why "ok",
"hi", that is one output, second time output, third time output.
So you have to understand first thing 'for' is some loop. Loop
means, you know, starting point and ending point same, right?
So you are repeating here only, that's
why that is called simply loop.
Okay. Then whatever the lines are there under your loop, those
lines are repeating certain number of times immediately, first
time, second time, third time.
Immediately it is repeating these two lines.
But how many number of times? Based on here
whatever you're providing in that how many number of values are there.
If it is a list and tuple how many values are there?
You can also write some string.
Let me do it string here.
[no audio]
I am writing simply "Narendra".
[no audio]
See the output. How many characters are there in a given string,
that many number of times these two lines will repeat again
and again immediately.
[no audio]
Right. Then, actually your syntax is 'for', this is any variable,
'each'. 'each' is there no, that is any variable.
You can take like x, y, z, ,p q, r, or your required variable, or
your name also you can take it. That is simply a variable.
Then 'in' is standard keyword.
You have to write 'in' as it is.
So here you can write a list or tuple or a string also.
Right. For time being what I am writing is, I am going to write
a list of values, four values
I am writing. Then these two lines will repeat four times.
No need to write two lines.
You can write 100 lines, or I can write simply one line
also, no problem. Just for our understanding purpose now
I am writing.
"Hello". Only one line
I am writing. Now this line will repeat again and
again immediately four times because you have four values
here. Observe the output first. Yes, you are getting. Now you
defined somewhere variable called 'each', right. Then
what is the use of this?
Actually, that is very, very important. Whether you
are going to use that inside of your for loop or not,
that is your wish. You can also use that in case if you have
a requirement. And one more thing guys, you are repeating this
lines, right, the lines which are there in your loop, if you
repeat once then that is called one iteration.
One iteration.
So now you can say here we have four values,
that's why your loop is iterating four times or your loop
is repeating four times the lines which are there under your
for loop. As of now, you have only one 'print' statement,
and in case if you want to use this variable inside of your
loop, you can use it. See that. What I am doing is,
("hello", variable), right?
You can print the variable value by using in this way. Now if
I run this, how it is going to execute? We know four times it
is going to execute because four values are there.
Immediately it will execute four times. Do that. Suppose.
I am writing here "before loop", and I am writing here
'print("after loop")'.
[no audio]
So between these "before loop", "after loop", 'print' statements
this line will repeat four times.
Right. First observe the output, how many times it is going to repeat?
"before loop", "after loop", between that you are getting four
times your 'print("hello")', but you are getting 'each' value as
different. Whenever it is repeating
first time, first iteration 'each' is value 2, second iteration
3, third iteration 4, fourth iteration 5.
[no audio]
Right. Then how your Loop is working with your variable
called 'each'.
Nothing is there guys. How your Python is going to execute? As of now
in your script you have only four lines, sorry three lines. Main
lines are three. Main lines are three. They are, let me take it one by one.
[no audio]
This is first line, second line, third line.
But this line is under your loop, under your loop, this
'print' line. Right. Fine.
Then how it is going to execute means, anyway whenever if
you run your script it will execute from top to down. So this line
will execute first, because of that you are getting this output.
Then whenever there is a keyword called 'for' then your Python
will assume that this is a loop, and your Python will decide
that I need to repeat some lines some number of times.
So as of now under your loop you have only one line. Now your
Python will decide that this line
I have to repeat some number of times, that number of times
are how many values you are having here.
Right. But how it is going to execute?
So whenever there is a for loop,
'for' keyword, immediately your Python will look here,
how many values or how many characters if it is a string.
As of now we have a list, right?
Okay, four values are there. Then your Python will take first
value first, and this value it will assign to this variable, then
after assigning whatever the lines you have under your loop
those lines will execute once. No while executing what I am doing?
I am printing 'each' variable also. Then currently what is the
'each' variable? 2 value. That's it.
That's why you're getting this one.
So first iteration is over. Then your Python will immediately
after completion of first iteration, 2 is going to store 'each',
then you are repeating this.
I mean your Python is executing this. Then after completion
of this line your Python will go and check immediately the
next value. Now, whatever the next value
is there again your Python will assign to your 'each' only.
Now your 'each' variable is 3. After assigning next value
to your 'each' variable,
again your Python will execute the lines which are there
under your loop.
But while executing you are printing 'each'. Now currently what
is the 'each' value? Second value.
That's why you're getting this one.
Okay, second iteration is also over.
Then your Python immediately will go for third value. Third
value is 4 no.
Now this 4 we'll assign to 'each' variable. After assigning
third value to 'each', then this logic will repeat third time,
and while repeating third time
what is your value? 4
That's why you're getting this one.
Then it will, Python will look for fourth value.
Yes, 4. Then 4 we'll assign to 'each'. Then we'll execute once.
While executing you are printing 'each' also. That's why we're getting
5. Then again your Python will try to look for next value
in the given list, but you don't have next value. Already
four values are over.
That's why your Python will come out from your loop and will
execute the next line, which is the main line in your Python
code. If four values are over then the execution part of this
line is over. Then your Python is going to execute this
one. That's why you're getting this line.
[no audio]
Right. So now if you have a list of values
from that automatically
you can take one by one value and you can print it using your loop.
Now this concept I am going to use for our task. How means,
how means, instead of writing this logic, see first of all
let me copy this code.
So previously you are taking only 'p1' and 'p2', only two paths
you're taking but you know in the given path you may have
any number of paths,
I mean files and directories.
That's why don't fix.
You don't know about that path, in that path
you don't know how many files are there,
how many directories are there.
How can you say only two paths, 'p1' and 'p2'?
That's why what I am doing is, right,
let me comment this logic as well.
This is simple for loop guys.
Okay. We have different ways.
We will see all those loops in the next section.
[no audio]
I am importing 'os', then
I am reading some path, after reading path, right, I am getting
a list of files and directories.
[no audio]
Let me take lengthy variable, 'os.listdir', your
given path.
Now, you know, the result of this operation is always a list.
That means this variable is a list. If it is a list I can
take one by one from that using for loop. See that, what I
am doing. 'for each_file', I am taking. See previously I have taken
only 'each' while explaining about your loop, but now I am
taking 'each_file'. Based on situation
you can take any variable. Now, from this variable list of
things I am going to take one by one.
So in that you may have files and directories, but I am taking
'each_file'. Instead of that 'each_file_or_dir' you
can take, but don't take this lengthy, this type of lengthy
variable. Okay. For understanding purpose
I am taking 'each_file_or_dir' from this.
Right. Now you have to understand that how many times this
loop is going to execute this 'print' statement.
You don't know how many times it is going to execute. It is
going to execute based on how many values you are fetching
into this variable. This variable result
you are getting from 'os.listdir'.
That means you are getting these values. Where it is? Where is your
Yeah, this values.
So indirectly you can assume a list of values in place of
this 'list_of_files_dir'.
So what your Python will take? It will take first value from
this and it will assign to this, and then we'll execute the
lines which are there under your loop. So I have only one
'print' statement that will execute. Then your Python will try to
take next item from this file or directory, and it will store
into this variable and we are printing that. That's why dynamically,
automatically you are going to print all your files or directories
from your given location. Now, you don't need to bother, you
don't need to worry,
how many files are there, how many directories are there. Automatically,
your Python is going to take care with the help of your loop.
Right. Now, let me go and run your code. Before that
let me open your code.
[no audio]
See this is your code simply.
I am going to provide some path.
Let me do one thing. I am going to remove all empty lines.
[no audio]
Let me clear our screen once again.
Yeah, this is your script. Right. Now
I am going to run your script, and I'm going to provide a
path called 'new'. Now,
see the output. You are able to display one by one file from
your list of values. If you want to see the list of values
you can print that as well. 'print("all files and dirs")'.
You don't know how many files are there and directories are there,
but that value automatically you are getting with 'os.listdir'
into a variable called 'list_of_files_dir'.
Now see the result.
[no audio]
First of all, you are getting this one.
From this list automatically your Python is going to take
with the help of for loop one by one value, and we are printing
that one by one value.
But my intention is not displaying simply your one by one value.
If it is a directory I want to display here,
"This is a directory".
"This is a file. "This is a file".
"This is a file". But before going to display, before going
to check whether that is a file or directory always, not only
here, wherever you are using
you should able to fetch complete path first.
So that's why what I am doing is, before printing I'm going
to create one variable called 'path'.
Let me take file or directory path, file or directory.
I don't know whether that is a file or path.
Right. See your given, if you add your given path to this
'each_file_or_dir',
so that you are going to get complete path, right. So directly
you can print it, but I am storing into one variable,
then I will, 'print(os.path.join)', your given path,
in that location one by one file
I am taking, and I'm adding that one by one file for your
path. Then I am going to print.
[no audio]
Okay. So guys for your understanding purpose what I am
taking is, this value I am taking. Okay. And I am copying directly
that into your script.
So instead of this 'list_of_files_dir' actually
you are getting that output as in this way. Just for, just to
give you the clarity. Right. Now assume that you're providing
path as something called,
this one you are providing, right?
Let me take directly that also in your script. Again, I will remove this.
Anyway finally we'll run our script. Now forget about your
previous logic. Now see here,
this is a path and in this path
we know, assume that
we know these are the list of values or items.
Now, what I am doing is I am going to take one by one from this
'for each_file_or_dir'. What your Python will do?
This first value it will assign to this variable, and the lines
which are there under your loop, those lines will execute
once. While executing what we are doing? I am taking one variable
and with the help of 'os.path.join',
I'm adding this path, see that, I am adding this path.
Then first time your file is 'mydir'. then 'mydir' I am adding here,
so that I am going to get for this complete path.
Then you are going to print that. Second time you are going
to take this one,
but again you are adding for your path
only. Your path is your given path.
So this path, then this file is going to give you complete
path. Likewise you are going to get path for all the files,
right? So actually what is the variable here we have taken?
Let me. Dynamically you have to to work it.
That's why don't fix any values here.
But internally you have to assume that these values are a list.
Then remove this one.
[no audio]
That is your logic, right?
If you want to check your script just this is your script
from here to here. Now I am running,
and I am providing a path. Now see the output. For each and
every file or directory you're getting complete path.
[no audio]
Right. But simply instead of printing in this way,
if it is a directory I want to print, "This is a directory",
if it is a file I want to print, "This is a file". Now inside
of your loop itself
I am going to add 'if' condition. 'if os.path.isfile',
your file or directory. If it is a file I can say 'f_d_p'
"is a file".
'else' if that is not correct,
that means that is not a file. That is not a file means then
you can say that that is a directory, no. "is a directory". That's
it. Save it and run it. But before going to run you have to
understand once again your entire code.
This entire part you are repeating for each and every value
which is there in a list. List is this one,
no. This is a list. You're taking one by one and for that
one by one you are finding complete path. With that complete
path you are executing this 'if-else' block. 'if os.path.isfile',
for your complete path,
if it is a file this will execute, then your first iteration
is over for first value.
Then you will take second value. For second value you are going
to get complete path.
Again.
Again you are checking 'if-else' condition. In case if 'isfile'
is a 'False', then you are printing this one.
Then second iteration is also over. Likewise
your for loop is doing this action for each and every file
so that finally you are going to get dynamically, for any
number of files and directories you are able to identify
the list of files and directories. See the output. "is a directory"
you're getting. "is a file" you are. Not only this path,
now you can provide any path.
Let me take some other path.
I am going to take suppose this as my path.
[no audio]
Run. Provide your path. Guys
this is Windows,
that's why I am providing '\\'. Same script
you can run on your Unix-like systems as well,
it will work without any error. In case if you are getting any error,
anyway you won't get it.
If you have any challenges, just let me know so that I will
give the complete script to work with your Unix as well.
But without any doubt it will work. See the result.
In the given location, first of all you're having this many
number of files, maybe directories or files.
You don't know which is a file and which is a directory, assume
that. Then your Python is able to form complete path. The path
whatever we are giving, from that your Python is able to add
this path. Then it is giving - "is a file", "is a file", "is a file",
"is a file", "is a file", "is a file", "is a file".
That's what it is working.
[no audio]
Right.
That's it.
So guys this entire explanation is just to understand your
loop concept.
Okay. See I have taken this example but my intention is not
to develop this script, to give your loop concepts.
So here I have given over all idea. In later sections
I will give step-by-step again your complete loop how to use
that, what is the complete syntax, in which scenario you are
going to use which loop, all those things
we will see. We have two Loops. One is for loop.
We have seen that. Next Loop is while loop. Along with that
to control your loop actions you have
some 'break' and 'continue' keywords. 'break' and 'continue'.
So this also I am going to explain with the help of examples
only. I won't go directly, and I won't give your syntax, and
I won't write your code directly by using your loops.
I will take a situation, then so that you can easily understand
what is the purpose of that, why we need it, right?
Okay. Okay guys,
thank you for watching this video.
[no audio]