[no audio]
Friends here we are going to do
a small practice with your 'os.walk'
function and that is, 'How to do a system-wide search
for a given file?' Assume that in your hand you have one file,
but you want to search that file path in your entire file
system, where it is located, that path I need to find out.
And one more thing while developing this script you have
to develop this script such a way that it should be like
platform independent,
that means same script should work with your
Windows, and as well as your Unix-like systems. For time being
first I am going to develop this script on your Unix-like
systems, and again we will develop a code on your Windows as well.
First of all, let me open my Unix-like system.
So this is my Unix-like system.
So guys, if you know the file system about your Unix-like
system, suppose if I give simply a root, this symbol, '/' then
this is nothing but your entire file system on your Unix-like system.
Now, if you take this as a path by default the meaning for
that is, you are going to look for entire system in your Unix-like
systems. Let me take one simple example.
I want to know where my,
[no audio]
something like 'host.conf' file is there.
See you are getting some 'Permission denied', because
root, you are giving entire file system means, you should
have root privileges.
So for time being just I am showing with your Unix command
lines, Unix commands.
Yes, if you have a proper permission, then you are getting exact
location where your file is there. Right. This thing
we are going to perform with your Python, and for time being,
right, here just to implement this script, for time being I
am entering into my root.
[no audio]
Now from here, I am going to develop my Python script just
to identify any file based on your requirement, right.
So what I am doing is, I am implementing a Python script, right
'search_a_file'.
in your entire system.
Fine. First of all, I am going to read the file name. What is the file
you want to search in your entire system, in entire file system,
right? Let me take 'req_file=', I want to read
a file name, only file name, right.
So, "Enter your file name to search: ".
[no audio]
Assume that user is going to enter your file.
Right. So guys, if you remember from our previous video,
if you have a path, then automatically you are able to find
out all the file names which are there in your operating system.
Right. See that.
First of all what I will do is, I will take for simplicity purpose
[no audio]
I will do one thing.
So this path I will take, for simplicity purpose first. Then
I will take root file system.
See using your 'os.walk',
right, that's why I'm importing our 'os'.
'for each', no, no.
Right. In your 'os.walk'
you're going to get three things. Root directory, directories
list, files list, 'r,d,f', in our 'os.walk' whatever the path you're
going to provide. For time being directly
I'm giving this path.
Then I am going to print your root
directory, then files.
So because of this you are going to get your root directory,
and the files which are there in that location. But I want
to print, I want to print each and every file with complete
path. Right. See that, what I am doing. 'for each_file in f'. Guys
if you are not able to understand this step, please go to
your previous video, and watch that video once again, then come back.
Then I am printing simply 'os.path.join()',
[no audio]
your root, then your 'each_file'. Suppose if I save this and
if I run this, see the script how it is going to work.
[no audio]
"Enter your filename to search: ". Anyway for that
we've not yet implemented any logic,
let me take simply 'test.py'.
See from your '/home/ec2-user'
automatically as of now
your Python is able to create all files, all files with complete
path. Somewhere you have your 'test.py', right?
Let me show you that.
[no audio]
Right. Yeah.
See, you're getting somewhere.
I want to print only this file because I am giving my searchable
file is 'test.py'. So, what I will do is very simple guys, 'vi'.
[no audio]
Before printing, if file is, see that, what I'm doing. 'if each_file',
if it is exactly equal to your 'req_file', then
only I am printing that path,
complete path I am printing. That's it.
[no audio]
'test.py'. Yes, you are getting complete path where it is
located. Not only that.
Let me take some other files which are having, let me first
display the files which I'm having in my, this location.
[no audio]
Let me take 'hi.txt',
and let me run our Python script, and let me provide the search,
your required file to search. You are getting complete path.
Now, as of now in your script whatever you have, 'os.walk',
that is taking path as some user 'home', but I want to
search an entire system, entire file system of your Unix-like
systems. Then I can take simply '/'. That's it.
Now, let me run it, and let me run and provide a filename
called 'test.py', you can take anything.
See, in this many places you have 'test.py'.
[no audio]
Now, let me run, and let me provide 'host.conf'.
Let me run, and let me provide something called, what I can say 'pip'.
This many places you have 'pip'.
Let me provide 'pip3'.
Not only that, anything you can get. Right. Now,
we implemented a simple Python script to search your file in
your entire file system.
So this is for your
[no audio]
Unix-like systems. Let's come back with
your Windows. Now here
I am going to implement for your Windows. Then finally
I will club those two codes. Okay.
Let me open my Sublime Text first.
[no audio]
Guys, if you observe, in your Windows operating system you
have drives. Based on drives
you have different partitions.
So that means there is no single symbol to identify your
entire file systems on your Windows operating system.
See, if you go with your Unix-like systems you have this '/' symbol.
So that '/' means your entire file system.
So this type of symbol is not there with your Windows operating
systems, right. Then based on drive
we need to search.
Based on drive we need to search our file in your Windows operating
system. But I don't know how many drives are there. Right. See, first
let me implement the code for one drive.
Okay. Let me 'import os'. Let me save it first,
[no audio]
'search_a_file'.
'.py'. Right. Okay.
Let me take 'req_file=', same thing, 'input("Enter
your file name to search: ")'.
[no audio]
That's it. Then path.
See guys, what I am saying is, let me do it
first. 'r, d, f in os.walk'. Here
I can't give any symbol like '/'. In Unix-like systems '/'
means it is going to work with your entire file system.
But whereas in Windows based on drives we need to search
our file systems.
So if you have a 'C Drive', then you have to provide 'C Drive'
name, and then you need to search it.
Right. Let's say, let's say I'm going to search from 'C Drive'.
Now what I am doing is, let me take again 'for each_file
in f', right.
'if each_file', if it is '=req_file',
then I can print complete path for that.
Right. 'os.
path.join()',
[no audio]
then your root in that 'each_file'.
Let me go to my command line, and let me run this script.
Let me open my command line.
So under 'Desktop', you have 'PythonScripts' directory. Now,
here you have, let me write 'python search_a_file.py'.
Yeah, this is the script. Now,
I am going to provide a file name called, randomly
I'm going to give it. I can provide anything. For time being
what I am giving is,
[no audio]
let me take simply 'even_od.py'. ''even_od.py'
So it's going to search. Because you have number of file systems
in your Windows 'C Drive',
that's why it's going to take some time.
But anyway, finally you are going to search only in 'C Drive'.
See on my system
I know suppose 'C Drive' is there, but in your operating systems,
you may not have 'C Drive'.
You may have drive name as some other name like 'D' or 'E' or 'F'.
[no audio]
Right. So guys because it's taking some time
what I will do is, I will provide a path like at least 'Desktop'
path, so that it will be easy for our script to identify quickly.
Let me provide path as 'Desktop' path, at least up to 'Desktop'.
[no audio]
You know whenever if you're providing a path manually on
your Windows operating systems, you have to provide '\\'.
That's what I am providing.
Now, let me rerun your script and provide the name.
See you're getting.
[no audio]
That's fine guys. But what is the problem
with your Windows operating systems is, manually you're
providing 'C Drive' path.
If you go with any Unix-like systems, complete file system is '/'.
[no audio]
Now I want to search an entire file system in your Windows.
There is no single symbol to identify that, then how you
can identify all your drives on your operating system.
Right. Nothing
is there guys. Before running, before searching any file on
your Windows operating system, logically if you want to
identify all the drives in your Windows operating
systems you need to write some extra logic whenever you
are working with your windows.
Let me take possible drives
generally. If you go with the possible drives they are from
suppose some 'a' to 'z', small letters and capital letters, right?
Let me take it in this way.
[no audio]
Right. But I don't want to write. I'm a lazy person.
I don't want to write a, b, c, d, small and capitals. Then what
I am doing is, I am entering into my Python terminal. Just observe here.
There is a default module called 'string', and if you observe
the operations with your 'string', there is something called
'ascii_letters'.
Right. Nothing is there. Simply 'print(string(),
that is the module, '.ascii_letters'.
[no audio]
Sorry. See the result.
Small letters and capital letters directly you are getting. I don't
want to type this one.
So nothing is there. Just 'import string' module. You have
so many things here, just observe them.
Now what I am doing is, I am also importing 'string' module.
Then possible drives names I am taking. 'pd_name=',
simply I can write in this way. Now,
let me print first,
Guys just observe that. Finally you can understand
what is the usage of this thing.
Now what I will do is, I will take one by one
letters from your 'pd_names',
I mean I'm taking one by one possible drive name.
[no audio]
Right. See.
[no audio]
One thing.
Let me take 'each_drive', each drive name
in your 'pd_names'. That is a 'string'.
So if it is a string I can take one by one character separately
with the help of your for loop.
Yes. You're getting one by one, right?
But now what I am doing is, see I don't have all these drives
on my operating system, right?
I have only some of them. But I need to identify which drive
you have in our operating system, from A to Z.
Right. So that's why what I am writing is, I am checking.
See 'if os.path.exist(each_drive)', before that
I want to, yeah, let me print. Because if it is 'C drive', right,
if it is a 'C Drive' how your path would be? 'c:\'.
Right. So now I need to form that
first of all. If it is a path then how it would be.
Where is your code? Yeah. 'each_drive +',
':', so manually you are writing,
it's better to write '\\'. Now see the output.
You're getting in this way your paths. But whether this is
a valid drive on your host or not
you have to check it.
This is like a path, right?
So what I am doing is, just simply I am applying 'if' condition,
'if os.path.exist',
if this is a valid path on your host
this condition will become 'True', then only I want to print
the drive name,
[no audio]
then only I want to print the drive name. Now see, as of now
your output is, all the letters you are getting, I mean all
the drives names you're getting but I'm checking the condition,
if that drive is there on my host then only print that.
[no audio]
Now see that. You're having only 'c', 'e', 'f', and 'C', 'E', 'F'. That means capital
and small drives names are same, there is no problem with that.
That's why, now
if you observe in output, I mean in the 'string' module you
have only either 'ascii_lower' or 'ascii_upper'.
You can take anyone of this. No need to take 'ascii_letters' now.
So I am going to take 'ascii_lowercase',
or if you want to take you can take 'ascii_uppercase'.
You have uppercase as well if you observe here.
Right. Anyone of that path is fine.
[no audio]
Right. Now see the output.
'C', 'E', 'F'. Now if I open my drive, my Windows,
see valid drives are 'C', 'E', and 'F'.
This is empty, right, that's why it is not there as of now.
That's why it is not able to find out G drive. 'C', 'E', 'F'
[no audio]
Now logically, I am able to find all my valid drives
on my Windows operating system.
Now if you run this part on your operating system, I mean
on your Windows
if you run this Python script, you will get based on your
Windows operating system
what are all your drives, that drives names it will display.
Right. Now I am taking a valid drives names.
Let me take simply 'vd_names', valid drives names as a empty
list. If it is a valid one then what I am doing is, I am collecting
that into one variable,
I mean as a list. 'append.each_drive', I am going to create
a complete path for each and every drive.
[no audio]
Right. After completion of your for loop finally just I am
printing. 'print(vd_names)'.
Let me take simply 'names' instead of 'name'. That's better.
Now see the output. Finally you are getting a list,
oh man, names.
Yeah. Now see the output.
These are all valid drives names on my operating system. Now,
actually my intention is I want to search about my file in
entire file system on Windows, right?
That's why first of all I need to identify all of the valid
drives on your Windows operating system.
Now I got them.
Now I don't want to print separately your each and every drive.
I collected all your valid drives as a list.
Yes, I have in my hand.
Now, what I will do is, guys be clear. Here
you may get confused.
What I am doing is, anyway I need to read required file,
right? Okay,
let me do it somewhere.
I want to read 'req_file'.
Yes, I will read. Now instead of this logic,
first of all I need to search in each and every drive, right?
First of all, I am collecting 'each_drive', 'each_drive' in your
'vd_names', and then suppose I am printing your
'each_drive'. See the output.
Okay, let me comment as of now this providing required path.
[no audio]
Yes. Right.
Now, let me comment this line as well.
So using your for loop
first of all you're able to get your drives one by one, 'vd_names'.
Now in each drive you have to search. So that
you can complete for all drives and in your entire file system
you are going to search.
I mean now you have to apply your 'os.walk' for each and every drive.
Nothing is there. See that. 'for r, d, f
in os.walk(each_drive)'.
So now first for loop is just to collect one by
one your 'vd_names'. Then this loop is just to execute
your 'os.walk' on each and every drive.
That's it. Now, let me do it, 'print'.
Yeah, you have list of files, right?
So 'for each_file in each_drive',
right, 'print(os.path.join(r,f))'.
Right. Now if I run this, just observe the output. Because of
this you are going to get all your 'vd_names', and
then in each and every drive directly you're going to print
all your files.
If I run this, see the output. It keeps on displaying, oh man,
not 'list'. What happened?
[no audio]
argument must be str or bytes, not 'list'.
'os.path.join()'. Yeah,
sorry, this not a, 'each_file'
you have to add it, right?
Yeah, it's fine.
See your operating system is able to display all your file
systems from your entire operating system.
Now.
Now, it is working on 'C Drive'.
I will wait some time so that you can see it will enter into
some, yeah, now
it is, still it is working with 'C drive' only.
Yeah, you have to wait some time so that your operating system
is going to get from each and every corner all the file names.
You can run the same code on your Windows operating system,
I mean your Windows operating system then so that your Python
script is going to give all the file names with your complete
path. Still it is working with the 'C Drive'.
[no audio]
Yes.
[no audio]
Just wait.
[no audio]
Each drive, drive names. Yes, fine.
Let me go and enable this 'vd_names'.
I will run from here then it will be,
it would be clear.
[no audio]
'python search_a_file'.
Yeah. Just give me some time guys.
Just observe the output.
[no audio]
It is going to form automatically
all paths for each and every file. Only for file we are writing
a logic. Actually, our intention is just to search a file. The same way
you can search for some folder
also, I mean folder or directory.
Right. Just wait and observe the output.
It's working now with the 'C Drive'.
After sometime it will enter into 'D Drive',
I mean 'E Drive', and then 'F', because I don't have 'D Drive'
Okay. Just observe the output.
[no audio]
Still your Python is working with your 'C Drive' only. You know
in Windows under your operating system, right,
you have a lot of, I mean the drive in which you have your
operating system in that you have a lot of files,
so it has to display all the files.
Yes, it is displaying.
[no audio]
Right. Just wait some time.
Finally, it will enter into other drivers as well.
So once it has entered into other drive, then I will stop and
I will write the remaining logic.
[no audio]
Still with 'C' only.
You have that many number of files,
that's why it's taking time.
[no audio]
Okay, still with 'C Drive'.
[no audio]
Because actually I installed so many softwares on my OS
and anyway by default with your operating system also
have so many files in your Windows,
so it is going to display all of them.
[no audio]
So same logic you can also use to print only suppose some
Python files, or some text file, something like that.
Okay. Okay, at least we have to wait up to the next level directory.
[no audio]
Do one thing, just run your script in your, I mean on your
operating system.
If you get any challenges, just let me know.
I will do it. So I don't want to waste your time by observing this output.
Let me stop this code.
[no audio]
Right. Now what I have to do is, I don't want to print each and every
file, right. If the file which is your required file, right,
'if each_file', if it is '=req_file',
[no audio]
then only I want to print that.
[no audio]
That's it. Now,
this is for your Windows operating system.
Right. Fine.
I will do one thing, from your 'C Drive' itself
I will take some file name. Then
that's good, right?
What I will do is,
[no audio]
any file name, right,
you can take any file name. Now
I am going to run/ Before going to run
let me enable which file you want to search.
[no audio]
Now I am running your Python script.
It is asking, "Enter your required file: ".
I am entering. Right. So it is going to look in your 'C', 'E', and 'F' drives.
So it's searching. Definitely you will get after sometime your required files.
Yes, the files which are ending with your file names you are getting them.
You have same file name in different locations,
you're going to get all of them. That's it.
So guys, this is the way how you can search your file in your
entire file system on your system.
But if we observe, drives are there only with your Windows
operating system, you don't have this type of drives on your
Unix-like systems.
Now your script should be platform-independent. How you
can make your system, your script as platform-independent?
See, if you observe, this is the code in our Unix-like systems,
right. Very simple code.
But in your Windows operating system you have this code.
So this code won't work on your Unix-like systems, and
that code, whatever the code you have on your Unix-like
systems, that won't work on your Windows.
Nothing is there. It's very simple. If you remember,
we have a 'platform' module, right, and let me show you it on
on your command line. If we 'import platform',
and if you 'print(platform
.system)',
what is the output? Your operating system name. Now based
on this what I am doing is,
right, so this part is for your Windows, right. If,
if your 'platform.system', if it is '=="Windows"',
[no audio]
then run the script,
run this logic.
So under 'if' condition
you have to write this thing.
That's why I am providing some indentations.
[no audio]
Right.
[no audio]
Let me modify your entire code, because for each and
every line you have to provide now indentation so that your
code would be there inside of your 'if' condition.
[no audio]
Then this is just to identify all your valid drives. Then second
thing you are taking one by one drive,
and then from your one by one drive, with the help of your
'os.walk'
you are creating all your files. Then after getting your one
by one file,
then you are checking whether that given file is matching
with anyone of your found file with the help of 'os.walk'.
If it is exactly a match then you are printing that. That's
it. 'else' if it is not "Windows", then what you have to
do? Just run your Unix-like system logic, this one. Anyway
reading required search file is common. Now very simple code
in your Unix.
[no audio]
Let me take one by one.
[no audio]
That's it.
[no audio]
One second.
[no audio]
So under this for loop you have a 'if' condition.
So under that I want to write, that's why I am providing some space.
[no audio]
Right. Again under this you have a 'print' statement,
then you should have some space.
[no audio]
That's it.
Now, whatever the code you are having here,
this will work on your Windows.
The same code will work on your Unix-like systems as well.
Let me show you first on your Unix-like systems.
Now, I created a Python script, and it is like platform-independent
script to search your required file in your entire operating
system. Let me copy it. Right.
Let me run it.
[no audio]
Let me provide 'host.conf' file
Yes, you are getting. Let me run it and provide some
[no audio]
'sshd_config' file.
Yes, you are. It's working,
right. Same code now
it is also going to work with your Windows as well.
Now your script is finally platform-independent script.
Right. So let me provide some file name which is there at very first.
[no audio]
Okay. Something, this is a text file.
Let me copy this name.
[no audio]
'.txt'. Right. Now your Windows is going to search on your
Windows operating system for your file, right. It is there
I mean inside of some 'I' letter directory, so it will take
some time to search for that.
You just wait. So finally it will find that out. Otherwise,
I will do one thing, very first based on alphabets, which
is going to 'r' recursively, right?
Yeah, fine. You have to take some time.
[no audio]
Hey, what happened?
It stopped here.
[no audio]
Let me take exactly the file name.
I will take simply,
'python.exe'.
[no audio]
Maybe 'python.exe'.
Yes, it's finding.
Maybe I have given some extension maybe not exactly
'.txt' for that.
So after getting your files from 'C Drive', it is also looking
for your other drives, that's why it is waiting.
It will take some time because it is Windows, right?
It will take some time.
But finally we implemented a script simply to identify, to
search a file in your entire file system of your operating
system, I mean on your system,
and that script is now platform-independent.
Nothing is there. With the help of your 'if' and 'else' condition we have
[no audio]
written a script. This part will execute on your Windows,
and if it is not Windows this script will execute. That's it.
Right. So guys this type of logics are very, very important in your real time.
And you can say this is best example from your 'os.walk'
for your real-time situations.
Okay. Okay guys,
thank you for watching this video. Bye.
[no audio]