[Tutor] getting around ValueError in os.walk

John Fouhy john at fouhy.net
Mon Jan 30 23:34:33 CET 2006


On 31/01/06, Andrew D. Fant <fant at pobox.com> wrote:
> when I try to run it, I get a "ValueError: too many values to unpack" which I
> think  comes from the fact that there is a subdirectory of /foo/bar which has
> over 2500 files in it.  The tree can't be easily restructured for legacy
> reasons.  Can anyone suggest the best way to get around this in code?

No, this means you are trying to unpack a tuple but you don't have
enough variables to do so.

example:

>>> x, y = (1, 2, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: too many values to unpack

Assuming the code you have given is copied-and-pasted directly, the
error is on this line:

> for (dirpath. subdirs, filenames) in os.walk("/foo/bar"):

Do you see that '.' after dirpath?  It should be a comma :-)  What
you've done is something like this:

>>> x.y, z = (1, 2, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: too many values to unpack

IMO, it's a somewhat misleading error message.  If the arities lined
up, you'd get:

>>> x.y, z = (1, 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'x' is not defined

which might give you a better clue as to where the problem is.

HTH!

--
John.


More information about the Tutor mailing list