[Tutor] getting around ValueError in os.walk

Alan Gauld alan.gauld at freenet.co.uk
Tue Jan 31 01:51:23 CET 2006

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

You need a comma after dirpath, python sees only two values
in the tuple but expects 3 (actually I'd expect a syntax error,
unless there is no space in the real code?)

> when I try to run it, I get a "ValueError: too many values to unpack" 
> which I

This is the standard message when Python tries to unpack a tuple
into fewer variables than are in the tuple.

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

> 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

2,500 files shouldn't cause Python to even blink.

BTW The OS topic in my tutor covers os.walk too.

Alan G
Author of the learn to program web tutor

More information about the Tutor mailing list