os.walk() ValueError: 需要 1 个以上的值才能解包 [英] os.walk() ValueError: need more than 1 value to unpack

查看:58
本文介绍了os.walk() ValueError: 需要 1 个以上的值才能解包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在使用 Bioloid Premium 人形机器人,Mac OS X 无法识别它.所以我写了一个 Python 脚本来检测我的/dev/文件夹中的变化,因为基于 Linux 的系统上的任何连接仍然通过文件描述符提供引用.我的代码应该可以工作,但是,当将三个变量分配给 os.walk(top) 返回的值时,我得到一个 ValueError.有谁知道我该如何解决这个问题?我以前用过这个功能,它没有给我带来任何麻烦.顺便说一句,我的剧本很粗糙,我写了大约 5 分钟左右.

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.

代码:

root_o, dir_o, files_o = os.walk(top)

错误如下.

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

我确实搜索了 stackoverflow,我看到的 ValueError 问题都没有引用 os.walk() 函数.

I did search around stackoverflow, and none of the ValueError issues I saw reference the os.walk() function.

推荐答案

os.walk 返回一个迭代器,它产生三元组,而不是三元组:

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',
      ...])

这篇关于os.walk() ValueError: 需要 1 个以上的值才能解包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆