My answer is very basic and plain. I am a beginner myself and found out my answers searching the web (see esp. the good documentation at docs.python.org) and trying some test code, such as this one:
for root, dirs, files in os.walk(startdir)
print ("__________________")
print (root)
for file in files:
print ("---",file)
This prints out the directory tree, where each dir—the starting dir and the included subdirs—is preceded by a line and followed by the files contained in it.
I think you have to keep in mind two things:
(1) os.walk generates a 3-tuple (a triple) <root,dirs,filenames> where
root is a string containing the name of the root dir;
dirs is a list of strings: the directory names directly contained in root, that is, at the first level, without the subdirs possibly included in
them;
filenames is a list of strings: the filenames directly contained in root.
(2) a for loop such as
for root, subdirs, files in os.walk(YourStartDir)
loops through root dir and all of its subdirs. It doesn't take a step for each file; it just scans the directory tree and at each step (for each dir in the tree) it fills up the list of the file names contained in it and the list of subdirs directly contained in it. If you have n dirs (including root and its subdirs), the for loop loops n times, i.e. it takes n steps. You can write a short bit of test code to check this, e.g. using a counter.
At each step, it generates a 3-tuple: a string plus two (possibly empty) lists of strings.
In this example the elements of the 3-tuple are called: "root", "subdirs", "files", but these names are up to you; if your code is
for a, b, c in os.walk(startdir)
the elements of the 3-tuple will be called "a", "b", "c".
Let's go back to the test code:
for root, dirs, files in os.walk(startdir)
print ("__________________")
print (root)
for file in files:
print ("---",file)
First loop: root is the dir you have given in input (the starting path, the starting dir: a string), dirs is the list of the included subdirectories names (but not of the names of the dirs included in them), files is the list of the included files. In the test code we are not using the list "dirs".
Second loop: root is now the first subdir, dirs is a list of the subdirs included in it, files is a list of the files included in it.
... and so on, until you reach the last subdir in the tree.
There are three optional arguments to os.walk: you can find lots of info about them and their use on the web, but I think that your question is about the basics of os.walk.