Alright, I'm working with a Bioloid Premium humanoid robot, and Mac OS X will not recognize it. So I wrote a Python script to detect changes in my /dev/ folder because any connection on a Linux-based system is still given a reference via a file descriptor. My code should work, however, when assigning three variable to the values that are returned by os.walk(top), I get a ValueError. Anyone know how I can fix this? I've used this function in the past, and it hasn't given me any trouble. My script btw is very rough, I wrote it in about 5 minutes or so.
Code:
root_o, dir_o, files_o = os.walk(top)
and the error is as follows.
Traceback (most recent call last):
File "detectdevs.py", line 15, in <module>
findDevs()
File "detectdevs.py", line 11, in findDevs
root_o, dir_o, files_o = os.walk(top)
ValueError: need more than 1 value to unpack
I did search around stackoverflow, and none of the ValueError issues I saw reference the os.walk() function.
Try this
os.walk
is a generator and you need to iterate over it.You need to iterate over
os.walk()
:or store the iterator first, then loop:
The return value of the callable is a generator function, and only when you iterate over it (with a
for
loop or by callingnext()
on the iterator) does it yield the 3-value tuples.os.walk
returns an iterator that yields three-tuples, not a three-tuple:Perhaps what's more useful here is where it says "more than 1 value to unpack".
See, in python, you "unpack" a tuple (or list, as it may be) into the same number of variables:
There are a few different errors that turn up:
Specifically, the last error is the type of error you're getting. os.walk() returns an iterator, i.e. a single value. You need to force that iterator to yield before it will start to give you values you can unpack!
This is the point of os.walk(); it forces you to loop over it, since it's attempting to walk! As such, the following snippet might work a little better for you.