gpt4 book ai didi

python - 使用 os.walk 在 python 中迭代不返回预期值

转载 作者:太空宇宙 更新时间:2023-11-03 17:29:04 25 4
gpt4 key购买 nike

我是 python 新手,我一直在尝试迭代一个包含大量具有一定深度的子目录的大目录。

到目前为止我得到了这段代码。

for dirpath, subdirs, files in os.walk("//media//rayeus//Datos//Mis  Documentos//Nueva Carpeta//", topdown=True):
for name in files:
f = os.path.join(dirpath, name)
print f

for name in subdirs:
j = os.path.join(dirpath, name)
print j

这个想法是使用迭代来对目录内结构的 Excel 文件进行 list 。

问题是,如果我只留下相同的路径而不使用“Nueva Carpeta”,它会完美地工作......但是当我添加“Nueva Carpeta”时,脚本运行时不会出现错误,但不会返回任何内容

最佳答案

import os

def crawl(*args, **kw):
'''
This will yield all files in all subdirs
'''
for root, _, files in os.walk(*args, **kw):
for fname in files:
yield os.path.join(root, fname)


for fpath in crawl('.', topdown=1):
print fpath

关于python - 使用 os.walk 在 python 中迭代不返回预期值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32160138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com