According to the Python 3 docs os.walk
returns a 3-tuple. However, this does not work:
root, dirs, files = os.walk('path')
Neither does this:
(root, dirs, files) = os.walk('path')
It always yields:
ValueError: not enough values to unpack (expected 3, got 1)
All the examples for os.walk
I found embed os.walk
in a for loop:
for root, dirs, files in os.walk('path'):
Why? What exactly will be iterated here? root
is a string, dirs
and files
are lists. However, most examples iterate again over dirs
and files
:
for root, dirs, files in os.walk('path'):
for name in files:
print(name)
for name in dirs:
print(name)
The inner for loops make sense to me, but I don't get what the outer for loop is for.
And why does the assignment to the 3-tuple work when os.walk
is embedded in a for loop, but not without one?