Reputation: 23
I am noob and trying to understand Python.
For os.walk documentation says that it returns a tuple (dirpath, dirnames, filenames)
just of understanding I am trying to use it like below
import os
from os.path import join, getsize
file=[]
dir=[]
xroot,dir,file = os.walk('C:\Python27\mycode')
But it gives me error like : xroot,dir,file = os.walk('C:\Python27\mycode') ValueError: need more than 2 values to unpack
My question is why cant I assign it like above rather then it being part of loop (most example use that)?
Upvotes: 1
Views: 283
Reputation: 298166
Your code attempts to unpack the generator returned by os.walk()
into a three-tuple. This is fine, but the problem is that the generator yields only two items, which isn't going to work.
Each item in the generator is a three-tuple itself, which is what your for
loop is really unpacking each iteration. A more verbose way of writing it would be:
for three_tuple in os.walk('C:\Python27\mycode'):
xroot, dir, file = three_tuple
You might find it easier to actually turn that generator into a list:
>>> pprint.pprint(list(os.walk('.')))
[('.', ['foo'], ['main.py']),
('.\\foo', [], ['test.py', 'test.pyc', '__init__.py', '__init__.pyc'])]
As you can see, the result is an iterable (a list) where each element is a three-tuple, which can then be unpacked into a root folder, a list of folders and a list of files.
Upvotes: 2
Reputation: 73608
os.walk
does not return root,dir,file
. it returns a generator object for the programmer to loop through. Quite possibly since a given path might have sub-directories, files etc.
>>> import os
>>> xroot,dir,file = os.walk('/tmp/') #this is wrong.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
>>> os.walk('/tmp/')
<generator object walk at 0x109e5c820> #generator object returned, use it
>>> for xroot, dir, file in os.walk('/tmp/'):
... print xroot, dir, file
...
/tmp/ ['launched-IqEK']
/tmp/launch-IqbUEK [] ['foo']
/tmp/launch-ldsaxE [] ['bar']
>>>
Upvotes: 1
Reputation: 304147
os.walk returns an iterator. The usual think to do is loop over it
for xroot, dir, file in os.walk('C:\Python27\mycode'):
...
but you can also just use xroot, dir, file = next(os.walk('C:\Python27\mycode'))
to single step through
Upvotes: 1