os.walk() ValueError: need more than 1 value to unpack

后端 未结 4 1743
滥情空心
滥情空心 2021-01-05 08:38

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

4条回答
  •  余生分开走
    2021-01-05 08:48

    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:

    a, b, c = (1, 2, 3)
    

    There are a few different errors that turn up:

    >>> a, b, c = (1, 2, 3, 4, 5, 6)
    Traceback (most recent call last):
      File "", line 1, in 
    ValueError: too many values to unpack
    
    >>> a, b, c = (1, 2)
    Traceback (most recent call last):
      File "", line 1, in 
    ValueError: need more than 2 values to unpack
    

    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.

    for root_o, dir_o, files_o in os.walk(top):
        make_magic_happen(root_o, dir_o, files_o)
    

提交回复
热议问题