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.
Verify the assignment variables. If the number of assignment variables is greater than the total number of variables, delete the excess variable from the assignment operator. The number of objects returned, as well as the number of variables available are the same. This will resolve the value error.
The “ValueError: not enough values to unpack” error is raised when you try to unpack more values from an iterable object than those that exist. To fix this error, make sure the number of values you unpack from an iterable is equal to the number of values in that iterable.
We get this error when there's a mismatch between the number of variables to the amount of values Python receives from a function, list, or other collection. The most straightforward way of avoiding this error is to consider how many values you need to unpack and then have the correct number of available variables.
The “valueerror: too many values to unpack (expected 2)” error occurs when you do not unpack all the items in a list. This error is often caused by trying to iterate over the items in a dictionary. To solve this problem, use the items() method to iterate over a dictionary.
Try this
for root_o, dir_o, files_o in os.walk(top)
print root_o, dir_o, files_o
os.walk
is a generator and you need to iterate over it.
os.walk
returns an iterator that yields three-tuples, not a three-tuple:
for root, dirs, files in os.walk(top):
# do stuff with root, dirs, and files
In [7]: os.walk('.')
Out[7]: <generator object walk at 0x1707050>
In [8]: next(os.walk('.'))
Out[8]:
('.',
['.vim',
'.git',
...],
['.inputrc',
...])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With