[no audio]
Friends, here we are going to write
one simple Python script. The purpose
of your Python script is find all files in a directory with
extension - '.py', or '.sh', or maybe any your required
extension. I mean while running your Python script first
of all it has to ask, "Enter your directory path".
After giving your path,
it has to ask, "Enter the extension of your required files".
Suppose let's say I am giving '.py' extension,
then it has to display only '.py' files in the given location.
Suppose if I give '.sh', then it has to display only
'.ssh' files in the given location.
Right. Let me go and write my script.
So as of now, I am going to open my
Unix box. Same script will run even on your Windows as well.
Right. See that.
Let me take some simple script as
'get_required_extension_files.py'.
See this is shebang. Sorry,
this is Unix-like system,
you can also write your shebang line.
So, this is my Python location, but don't write this whenever
if you are running your script on your Windows systems. It
will work only on your Unix-like systems.
Okay. Now the first thing is our script has to read a path. Path
is always a string. We know in Python 3 to read your path
we have, let me take 'usr_path' or 'req_path'.
I am taking 'req_path', right. 'input(:Enter your path: ")'
"Enter your directory path:",
That's it.
Then, after giving your directory path immediately you have to
ask the user, required extension. Now in that given location,
what are the files you need, with what extension you need,
right. That's why, "Enter the required files extension: ".
[no audio]
Example, I'm giving '.py', or '.sh', '.log', maybe anything
based on user requirement,
maybe '.txt', whatever it may be.
That's what I want to read from the user.
Fine. So first of all user is, okay, as of now you are going to
run this script. See that, how it is going to work.
Let me take some path, this as my path suppose.
Let me come out from here.
Then I'm going to run our Python script. Before going to run,
let me provide execution permissions. Now because this is
Linux system, after providing your permissions, after giving
your shebang line,
you can run your Python script with './'.
So it is asking, "Enter your directory path: ".
Now, I'm entering suppose this one, any path,
your required both path you can give. After that
it is asking, "Enter your required extension for your
files: ". I'm entering suppose '.py'. Your script is ending,
because as of now we don't have any logic to find out your
extensions, your files with extension.
Only we have a logic to read path and then to read your extension
of a file. Fine.
See suppose given path, by mistake suppose if user
enters a file path.
Right. See suppose if I take this as a path, this is directory
path suppose.
Now if I take suppose something like, we don't have anything
here. Yeah, 'get_req', this, if I give this one, right, this is
not a directory. If it is not a directory you don't have anything inside of that.
[no audio]
So that's why this path is always a directory.
So what I want to do is, after reading your directory path,
I'm checking whether this is a valid directory path or not.
So it's a good practice actually to write that logic.
So before reading your extension, right, I can also write
my logic after reading extension, but before reading extension
I am writing my logic. That is, 'if' given path is file or directory
I want to check it. You know in your Python you have a
'os' module. In that 'os' module
we know we have 'os.path.isfile' or 'isdir'.
So now what I am checking is 'os.path.isfile', if it is a file
I want to stop my script and then I want to say to the user,
"Please pass only directory path".
Okay. So guys this script I am implementing just to give
idea about your modules and some concepts. Okay.
You may write the same code in a different way.
No problem. Fine.
Now, what I am doing is, in case if the given path is a file,
then what I want to say to the user? 'print(
[no audio]
"The given path", whatever the user is going to give, "The given
path", that path,
is a file.
[no audio]
Please pass only directory path")'. That's it.
[no audio]
I am closing. Then, 'else', 'else', right.
So here I need to write my logic.
So for time being I will write this also, so that you will
get some ideas.
I am writing here. Now,
let me remove this path here.
So here I need to implement my logic. As of now
I'm not implementing.
We'll come back and we'll implement.
Okay. So, "implement your actual logic here". We'll implement it
after some time. Just wait. Now, let me save it. Right.
So what I am doing is, let me do one thing. Under 'Udemy'
let me take some,
'read'.
[no audio]
I will do one thing.
[no audio]
Okay, come back.
And this is your script, right. Now,
I'm running that. So it is asking, "Enter your directory path: ".
But suppose, just assume that I am going to give a path called,
[no audio]
in this location I am going to give some files path.
Let's say, it may be anything, then script is saying that, see
that. "Enter required",
okay, "required files extension'
Let me enter '.py'. "The given path is a file. Please pass
only directory path." Suppose if I enter, if I enter only directory
path. Let me take,
[no audio]
this one.
Now your script will display
[no audio]
"implement your actual logic",
we need implement that part.
Right. Now see, one more thing.
[no audio]
Here we are going to implement our logic.
[no audio]
Okay. First of all what I will do is, I will list all your directories
and files in the given location. See in 'else' block only, because
in 'if' condition what we are checking? If it is a file then
we are displaying this information. You know, if this is
'True', then your 'else' block won't execute.
If this is 'False' means you are giving valid directory path.
Then you are going to execute this one.
Right. Now see what I am doing is,
[no audio]
'print'. See in the given location
if you want to list all your directories and files,
if you remember, there is a operation called 'os.listdir()',
then your required path.
[no audio]
Okay, let me save it, and run it, and see the result.
I'm running, and I'm going to provide your path as
[no audio]
directory path. Okay. Let me provide extension '.py'. Yes, in the given
location you are getting all the files which are there,
but actually our intention is, we have to display only
suppose '.py' or '.sh', whatever it may be, whatever the
user is going to enter, that extension files we need to identify.
So now what I have to do? I have to take one by one file
from here and I need to check it whether it is ending with '.py'
or not. You know how to take one by one file from a list,
by using for loop you can take it.
But before that, let me tell you one more thing. See whatever
the path you are going to enter, if it is empty then why should
we check whether file is ending with '.py' or not?
Right. So that's why what I am doing is, before writing your actual
logic I am checking,
I am checking. First
let me find out all files and directories, all files and directories
in the given location.
Then after that I am writing one more 'if' condition inside
of your 'else'. That is, 'if len()', of, you know what is the 'len()'
function. It is going to find out number of values in the
given list. You know this operation output is a list guys. Now
I'm checking how many values are there in this. By mistake
if you have 0 values means there are no files and directories
in the given location.
So at that time I don't need to find any logic, right. So I
am writing simply, 'print(
[no audio]
"The given path is an empty path")', so that path I am writing here.
What is the given path? 'req_path'. That's it.
[no audio]
Let me check it whether it is working perfectly or not. 'else'
'else', then we have to write actual logic.
So then, 'else:', here I am going to write 'print("implement your
logic")'. So as of now we are not implementing. Yes,
we will implement it, right. Just wait.
Now there is a directory called 'empty'.
I am taking this 'empty' directory, and then let me take this
one, and also that path, then come back, then we have our file
here. Let me run it.
Oh man, one extra parentheses.
Let me remove that.
[no audio]
See that directory path
I'm going to in this remove this parentheses. That's it.
Right. Fine. Now,
I am running this one. See the output. "Enter your directory
path: ". I am entering suppose "Udemy" path. Now see the output.
Okay, "Enter the required files extension", as well.
Let me enter it. "implement your logic". Actually,
this is not an empty directory, right?
That's why
"implement your logic". Now what I am doing is,
I am going to enter 'empty' directory path now.
[no audio]
Now see the result. "The given path is", whatever the path, "an
empty path".
[no audio]
Right. That's why no need to find '.py' files.
But if you observe your logic clearly, let me open it first.
[no audio]
Before checking whether the given path is empty or not,
you're also reading required file extension.
Now in case if your given path is an empty path,
why should we read this extension guys?
That's why now
what I am doing is, I am going to comment that line, or I am
going to remove that line there and I want to write somewhere
else. Where I have to write? If your given path is having
some files then only I want to read extension.
[no audio]
Right. See if 'else' block is executing means,
[no audio]
inside of your 'else'
you have one more 'if-else'. If this 'else' is executing means
you have some list of files in the given location, then only
I want to read an extension.
[no audio]
Right. Now see, let me save it, and run it, and before going
to run let me take my path, this one.
Now, I am providing 'empty' path. Now directly
it is giving, "Given path is an empty path".
Now, if I give you some valid, I mean path which is having
some values, now
it will also ask, "Enter your extension", right. If you give empty
path it is not asking. If you give some path which is having
some values, some files and directories, then only it is asking
"Enter your extension: ".
That's good actually.
So unnecessarily we are not going to read extension if your
given path is an empty.
Fine. Now what I am doing is, after reading your extension our
requirement is, we have to take one by one file from all your
files and directories.
See I am taking 'for each_f in', your
list of files
[no audio]
Let me first 'print
[no audio]
(each_f)', in the given location")'.
[no audio]
Right. Let me run it.
[no audio]
'./', I am going to run. It is asking, "Enter your path: ".
Let me enter my path as suppose this one.
Yes. Now I am giving extension as '.py', but your script is
printing all the files, right, files and directories.
I don't want to print all. The files which are having, which
are ending with '.py', those files only
I want to print. That's why again in your 'if' condition, sorry,
again in your for loop you have to add a 'if' condition before
printing your files. See what I am doing. Before printing your
file 'if', if you remember string operation is there, that is
'endswith', your path is always,
your path is always string. Now file is a string.
Directory is a string. Now simply I am doing, is it ending with
'.py' extension?
If it is really ending with '.py' extension, then only
I want to print, otherwise I don't bother about that, right.
Now, let me run your script and see the result. Now
I am giving path as, and I am giving '.py'. See the output.
You're getting only '.py' files. Now,
let me run your script once again, and let me provide extension
as suppose '.sh'. See the output.
[no audio]
The problem is directly I fixed, extension is '.py', but
what about the path extension you are reading, that variable
you have to write now.
So actually this, in this you are storing your extension, right?
Now it will work.
[no audio]
Let me enter your path first.
[no audio]
Let me enter '.sh'.
Yes, you are getting only '.ssh' files.
Let me rerun and provide suppose '.py'. Now you're getting
only '.py' files.
Right. Fine.
And if you observe your script, your script is displaying
individually your required files, but I don't want to print
individually. I want to print all your '.py', '.sh' as a list.
[no audio]
Then how you can do it?
Right. See guys whenever if you want to create a list based on
your required values, first of all you have to define somewhere
an empty list.
So after reading your extension what I am doing is, I am
defining an empty list. That is,
let me take
'req_files = []'.
[no audio]
Instead of printing
what I am doing is, inside of your for loop I am appending
your each and every, your each and every file into your list.
If you remember there is 'append' operation with your list.
The purpose of 'append' operation is adding your given values
to your list. That's it.
Right. And then after completion of your for loop, I am going
to print finally, "The required",
see the result, "The required files are", as a list I am printing.
[no audio]
You have taken 'req_files'.
[no audio]
'r-e-q', right. And I did a mistake in your loop,
let me go and modify that variable name as 'req'.
[no audio]
Right. Fine.
Let me save it, and run it. I'm running. I'm going to provide
a path called,
Where is my path?
You can take any path, right. As of now, I am giving this path,
and I'm giving '.py' extension. See the result. Now instead
of printing your files separately,
you are getting as a list of your required files.
Not only this, same script will work for any type of files.
Let me enter, and see the result.
I'm entering suppose
'.sh'. Yes. fine. Guys
now final concept is, I am giving a path called this one.
[no audio]
Suppose I want to get a list of some batch files. See the
result. "The required files are: []", you are getting because
there are no '.bat' files in my given this directory.
See the result. Here
we don't have anything which is ending with '.bat'.
If I don't find any files ending with '.bat' in the given
location, I don't want to print in this way.
I want to print like, "There are no .bat files in the given
location", instead of printing an empty.
Nothing is there. It's very simple. See that. What I am doing
is, before printing, before printing your required files from this list
I am checking whether that, whether that is having 0 values
or not, 'len(req_files)' if it is equal to 0,
then I want to print, 'print(
[no audio]
"There are no",
[no audio]
whatever the extension you are providing,
that extension files,
[no audio]
"files in the location"
[no audio]
"in the location
of", you can write your message in any way.
What is your required given path? This one, right?
[no audio]
That's it.
'else', 'else' then only, 'else' means not equal to 0, means
there are some files. Now I am printing that.
[no audio]
See the result. Now
I'm going to run your script.
Let me copy your path.
I'm giving your path. Extension
I'm giving '.bat'.
There are no '.bat' files in the location of your, that one.
Let me rerun and provide '.py' extensions.
Yes, you are getting.
[no audio]
Right. But, one more just for your decoration
purpose, I want to write one more line.
That is, if you have some values, I mean if you have
some '.py' files, how many files you have in that?
Right. See nothing
is there. Again, it's very simple.
[no audio]
I am printing with one more 'print' statement, 'print(
[no audio]
"there are",
"There are ")'.
That means it is going to give a count, how many values
are there in that list.
"There are", this many number of files, "files in the location of",
[no audio]
required path, "with an extension of",
[no audio]
whatever the given
extension, right, that is there in this.
[no audio]
That's it. Fine.
And, "They are", now instead of writing
"The required files are", I can write,
[no audio]
"So, the files are". "So, the files are". That's it.
Let me save it and run it.
[no audio]
I am giving a path of this one, now
see the magic. '.py'. "There are 5 files in the location
of", "with an extension .py". Then,
"So, the files are", you are getting those five files.
Not only this, right, you can provide any path and any file
extension, automatically now our Python script will fetch them.
Only two files and we are getting them. Right.
So guys, this is the simple way to get your required files
with your required extension in the given location.
See, here maybe concepts are simple,
but the thing is, you have to understand that why we are taking
first 'if-else' block, and why we are writing the code only
inside of your 'else' block of your first 'if' condition.
First this is our main code guys.
If you want to understand this code you have to understand in this way.
If you run, your code will execute from top to down, right.
You have three lines, main,
sorry, one, two, three, four, five, main lines.
In the five main lines whenever if you run your script, so
by this line, your operating system will understand that
the remaining code has to run with the help of your Python 3
Then you are importing 'os'.
Then you are reading your path.
Then you commented this. Don't worry about this line. Then
'if' and 'else', so first of all, you have to understand that, 'if' condition
is 'True', this line will execute, 'if' condition is 'False', this
entire block is going to execute. But inside that again we
have one more condition, that's a different story.
If given path is a file, then you are going to execute this
line. Then you don't, then
you don't have any lines to execute.
If this is 'False' means, given path is directory.
[no audio]
Right. Then you are going to execute these lines.
[no audio]
Right.
Then you are finding the number of files and directories
which are there in the given path, right.
Then maybe you have some files and directories in the given
location or you may not have anything in that, that's why
I'm checking, 'if', all files and directories in the given location,
the number of values, if equals to 0, no need to find the extension
files. Right.
That's why 'if' and 'else'. 'if', number of values, I mean number
of files and directories are equal to 0, then you are printing this one,
then your script is over.
If it is not equal to 0 means, there are something, maybe files
or directories or both. No problem.
So if there are something then only I want to read extension.
Yes, I am reading that.
[no audio]
Right. So this is just variable.
You're defining an empty list.
Then, you're going to get some number of files and directories
in this, right. Now my intention is I want to take one by one,
and then I want to check whether this is ending with your
required extension or not.
If it is ending with required extension, then I am storing
that value into this list. You know in list you can store
any number of values.
Right. After completion of your for loop what I am doing?
[no audio]
Right. 'if len(req_files)', whatever the files
you are finding. If it is equal to 0 means, there are no '.py'
or '.sh', whatever may be your extension, with that extension
you don't have any files.
That's why I am printing this information. 'else' means, you
have some files, then I am printing that, how many files are
there, and what are those. That's it. And guys in this entire
program if you provide valid path then only it will work. Suppose
if you enter an invalid path, see that. First of all
[no audio]
I am entering now path as,
[no audio]
something. See that.
"No such file or directory".
[no audio]
Right. That's why you have to provide valid file. In case if you
provide invalid file or invalid path, then how to take
care? We will see that while going forward.
As of now while running this script
you have to provide valid path only. It maybe directory path
or file path.
If you provide file path, then your script will stop and
say that, "This is a file path, please pass only directory
path". If you provide valid directory path, then it will work
on your given path.
Right. So
if you provide any invalid path how to handle, we will
see that while going forward.
I don't want to confuse you in a single video with all those concepts.
Right. Fine.
Okay guys, thank you for watching this video.
[no audio]