Friends, here we are going to
work with 'os.walk'. See this 'walk' is
a method from your 'os', and it is used to generate directory
and file names in a directory tree by walking.
See simply in case if you are familiar with Unix-like systems,
and if you know 'find' command, right, suppose if I run something
like 'find path -name *',
let's say some
'.txt',
[no audio]
so what it will do? Your 'find' command
will recursively look for text files from the given path.
Means, assume that you are giving a path like this something
like, some 'xyz' user you are giving. Then first
it will look for your text files in this path.
Then if there are any directories, let's say I have a 'dir1',
then automatically your 'find' command will create a path
called '/home/xyz/dir1', and in that also it will try
to look for text files. And assume that in 'dir1'
also you have some directories,
let's say 'sub1', then in that location
also it will try to look for '*',
I mean any text files.
So from the given path it is going
to look for some text files recursively, that means all
the possibility paths whatever you have inside of your path,
recursive paths, in all those paths
it will try to look for your '.txt' files. That's fine.
So the same way you're going to work with your 'os.walk' as well.
But in case if you don't know about 'find' command, don't worry,
here we are going to discuss about it.
Guys, let me take some path.
I am going to take a path called, let me take, yeah, this is a path.
Okay. Now, I'm going to write a simple Python script.
[no audio]
Okay. Let me write any script name.
I have given script name as 'os.walk.py'. Okay,
'os_walk.py'.
Let me take a path called,
this one. Right.
Just now we copied. We know we are working with the Windows,
we have to provide '\\' in your path,
otherwise because of special characters you may get error.
Right. Fine.
Now let me directly print your
[no audio]
'os.walk', then your given path, and we are using here 'os' module,
so before using it you have to import it, and it's better to
import at very first line. That is actually better practice.
Fine. Now, I am running. You're getting some output, and this
is a 'generator' object.
You can also convert your object into list because this output
is finally going to give a list.
That's why I am trying to convert this as a list. Now see
the output what you are getting.
[no audio]
First of all from your given path Python is generating a
list which consists of tuples, and each tuple consists of
three values. Be clear.
'os.walk' is always going to generate a list, assume
that. Actually that is a 'generator' object, but you can convert
that into a list. Now, it is very easy
if you remember in this way. 'os.walk' is going to
generate a list first, and that list consists of tuples.
See that, this is a tuple no, and that tuple consists
of three values. First value is always the path which you have given,
and second is a list, third is also a list, and
this list is list of directories in the given list, in the
given path. In this path, the list of directories you will
get here. Here you will get the list of files in a given directory.
Automatically your Python is separating,
right, your list of directories and list of files for your given path.
[no audio]
Right. Let me take other path.
So I am going to take a path called suppose, 'programming',
[no audio]
some different path, in that path
I have only files, right?
Anyway I have only one file. If you want to create one more
file, let me create 'x.txt', 'y.py', something like that.
Now you have three files here.
Okay. Instead of 'scripting',
let me take different path. Now
I am going to run. See the output.
Still you don't have any directories, but you have files
in the given location.
Now your Python is able to identify them.
[no audio]
Right. Now, let me take one directory here.
[no audio]
Let me create directory,
some 'hello' directory. Right. Now in your 'programming',
you have a directory. Right.
Okay. Let me run it and see the output.
[no audio]
You're getting two paths. One path is your given path. Be clear.
This is what your 'os.walk' is walking, doing.
In your given path
you have one directory and three files. That is over,
whatever the path you are giving. But your Python will recursively
search from there onwards.
See, because 'hello' is there, if I add 'hello' path to this path,
then you can create one new path.
That's what Python is doing.
Automatically this 'hello' is going to add to your given path.
The new path it is adding no. New directory path is there.
Now in that new directory path
it is going to list directories and files.
As of now we don't have anything.
Let me create one more directory and see the result.
[no audio]
Sorry, that is a file, right?
Let me create one more directory, it's already there.
Yeah.
[no audio]
Oh. Okay fine.
Now let me rerun and see the output.
Guys, if you observe clearly for your given path,
first of all your Python is giving this as your path, and
in that path the list of directories, and then in this path
list of files. That is over with one step. But if you add
these two directories to your given path, what will happen?
New directory path will be coming up.
So yes, you are able to form new directory paths.
Now Python will automatically form that, and in that
if you have any directories and list, it will list out. Right. The same way
it is forming two paths with 'byee' one path,
'hello' one path. Suppose inside of your 'byee', or 'hello'
if you have one more directory, let me travel into my 'hello',
as of now we don't have anything here.
Right. What I am doing is, first I am going to create a file called,
'hello.py'.
'hello.txt'.
Right. See, previously with your 'hello' directory path,
you don't have any files here, right. Now,
I have files, two files, right. See that. Now, let me run our
script and see the result.
Now you've got two files automatically.
[no audio]
And here I don't have any directories.
Let me create a directory,
'hello1'.
[no audio]
Now see the result. You're going to get one directory here,
at the same time because this is a directory, this directory
your Python is going to add to this path, and it is going to
form a new directory path,
that means all possibility paths
it is going to form automatically the path which you have
given from that. See that, 'hello' directory,
'hello1' directory, in that two files. But this directory
if you add to this path, then you are able to create new directory
path. That's what Python is doing
when it is giving a list of files and directories. But we don't
have anything here. Likewise,
if you give any path, from that path your Python is automatically
forming all possible paths, and each and every path
it is going to provide list of directories and list of files.
Now assume that, observe that, whatever the output you are
getting that is a list. Be clear.
Basically, this is the list symbol, right. In that list
if you observe, you have a tuple, this is a symbol, right?
And this is up to this one only.
This is the first tuple in a list, and this is the second tuple,
and this is a third tuple, fourth tuple.
See if you have a list, right,
you can take one by one using your loop.
That's why what I am doing is,
instead of printing in this
way, okay, otherwise I will do one thing.
I will keep this as it is. Again,
I will apply for loop, 'for each in' your
'list(os.walk
(path))'.
Let me print your 'each' and see the output.
Try to understand.
See guys, let me differentiate them.
[no audio]
See in your first output, 'os.walk(path)', directly
I am printing. In that this is first tuple value.
In your list this is the one value. I am able to separate them using for
loop. Same way second.
Now I am printing separately your tuple values from your
list. And one more thing because this is a generator you
are converting that into list just to see and to understand
by you, but Python can understand that without converting
into a list. Now see that, I am removing this list.
List operation I am removing. And if I run, see the output.
Same output, means Python can understand that
what is there inside of your generator?
Actually, that is a generator object, right. Python can understand
that. That's why you're getting this all possibility tuple
values. So you are getting four tuples. Right. Fine.
Now, let me comment your 'print' statement, and observe the output.
Four tuples you're getting, right. Guys in each tuple
if you observe you have three values - one is the path, and
second one is in this path
what are the list of directories, and third one is in that
path what are the list of files.
Yes, you are getting, right. Now,
if we have three values in your tuple, let me show you that.
[no audio]
Try to follow step-by-step.
Let's say I have a tuple called in this way.
'x=(3, 4, 5)'. Then if you print 'x', we are getting in this way.
But '(3,4,5)' directly
I can assign to three variables, and see the 'y' value or 'z'
value or 'x' value.
You can unpack your tuple directly into a number of variables,
but while assigning in this way the values which you have in the tuple,
the number of values should be the number of variables so
that you can unpack.
Now what I am doing
is, if you observe your output in your script,
the first one is some given path, right, or assume that
this is a root path.
I mean, whatever the path you are giving that's just assumption,
root path. Then in that path, directories list,
in that path files list.
So now this is a tuple, whatever the value you are printing
using your for loop, each and every value is a tuple.
Now, what I am doing is,
[no audio]
directly here itself I'm unpacking. See instead of 'each'
what I am taking is your root path, directories list, and then
files list. You can also take lengthy variables.
I don't want to take in this way like 'root', 'dir_list',
'files_list', I don't want to take this lengthy variables. Simply
I want to take like root, and these are not the fixed variables.
You can take any variables.
[no audio]
That's it.
Now if I print all these three you are getting your tuple. Right. Now,
what I am doing is, I am printing only 'r', then you can expect
from each and every tuple you're going to print this first
path. That means all the possible paths you are printing
Let me run it and see the output.
You're printing only all possible paths.
Now I want to print each and every path,
what are the files list, only files list. The path, in that
path what are the files. See that 'r, f'. That's it.
If you have, you will get it. If you don't have, it won't display that.
Now I want to get each path, in that path only directories list.
See that.
[no audio]
Fine.
Okay. If it is fine, now let's come back with again files.
[no audio]
See the output. The path from you, guys
first of all you're getting your given path, in that path
list of files.
I have one more option here.
[no audio]
'topdown =
'False', and in this case, what is the output, just observe that.
You are reversing your output.
So your Python is going to get from bottom to top values.
By default it is always 'True'.
'topdown' is 'True'.
Even if you write no problem.
Even if you don't write no problem. See if you write you're
getting suppose 'bye', 'ok' first. That is by default. If you want
to make it as 'topdown' as 'False'.
I mean, I want to get output from down to 'false'. In your directory
structure, suppose assume that this is a given directory. In that
you have some number of directories and then number of files.
Again in this you have some directories
and files. Again in this directory you have
some number of directories and files.
So this is top-down. In this way
your Python will look by default. In case if you want to print
your paths from down to top, then you have to make it
'topdown' value as 'False',
so that your Python will give first this list, then come back,
then come back, then your top. That's it.
But it's not, I mean not a required method. If you want
you can mention this option, right?
That's fine.
Okay. Now you are getting your path, in that path
you have your files.
Right. Now I want to print the path which consists of some files.
I don't want to print all the paths which are not having
files. Then what I will do? See that. 'if len(f)',
if it is not equal to 0, if it is 0 I don't want to print,
if it is not equal to 0 then only print. See that, you are
getting only two paths which are having your files. Previously
you got four paths, right, but now you're getting only two paths. Because
I have given a condition,
print the files, print the path and files which are having,
I mean the path which are having some files.
So what I am doing is, now first of all you know you are able
to find your list of files in a given path.
Then I'm finding length. If length is not equal to 0 means
you have at least one file. Then I am printing that.
Right. Fine. Now
I don't want to print, I don't want to print my files as a list.
I want to print my files one by one.
First what I am doing is, I am printing my root path.
Right. Now let me print first separately your files. Guys,
I'm going with step-by-step. The same way you just practice,
and observe the output.
I am getting my root path,
I mean for these two files whatever the path is there. Then
for these files, whatever the path, path and it's files I am getting. Now,
I want to get, I want to display one by one file under
your given path.
Nothing is there. You can take for loop here again.
'for each_file in f',
just simply print your 'each_file'. See the output.
[no audio]
Now what I want to do is, I want to get complete path for my each and
every file. It's very, very important.
I want to get each and every, each and every file
I want to get complete path. See guys if you clearly observe,
in this path only you have your files, right. If I add this path
to your 'each_file',
you're going to form complete path.
So this scenario is very, very important in your real time.
Nothing is there. Directly you can print 'os.path.join',
your root, then your file.
[no audio]
It's very simple. See the output.
You are getting your root path in that, I mean root path,
now I am getting complete path for my each and every file.
Right. Just for your differentiation between different paths
I am writing here hyphens.
Now see the output.
This is your main path, in that the files, but I am getting
with complete path.
That's what actually I need. It's very, very important. Then
second path and in that path I have list of files.
Right. Fine.
So guys, whatever the concept we've done in this video,
this is applicable even for your Unix-like systems.
Okay. So in next video, we will see with practice,
with real-time practice.
Okay. Okay guys, thank you for watching this video.
[no audio]