【发布时间】:2022-12-01 10:44:16
【问题描述】:
我正在尝试创建一个字段结构,但在 .items() 中使用包含 3 个条目的 for 循环时遇到问题。
for pais, tipos, nombres in dirs.items():
path_pais = os.path.join(new_path, str(pais))
if not os.path.exists(path_pais):
os.makedirs(os.path.join(path_pais), exist_ok=True)
for tipo in tipos:
path_tipos = os.path.join(path_pais, str(tipo))
if not os.path.exists(path_tipos):
os.makedirs(os.path.join(path_tipos), exist_ok=True)
for nombre in nombres:
path_nombre = os.path.join(path_tipos, str(nombre))
if not os.path.exists(path_nombre):
os.makedirs(os.path.join(path_nombre), exist_ok=True)
我有这段代码,当运行它时我得到 ValueError:没有足够的值来解压(预期 3,得到 2)。 我知道 .items() 只能有 2 个条目。我在 dirs.items() 中尝试了 pais,( tipos, nombres): 但是我得到 ValueError:没有足够的值来解压(预期 2,得到 1)。 我能做些什么 ?
【问题讨论】:
-
您应该使用
pathlib而不是os.path。更优雅!
标签: python for-loop valueerror items