【发布时间】:2018-08-21 18:26:56
【问题描述】:
列出当前目录下所有带有 ext .txt 的文件。
L = [txt for f in os.walk('.')
for txt in glob(os.path.join(file[0], '*.txt'))]
我想避免来自一个特定目录及其子目录的文件。假设我不想深入研究folder3 及其可用子目录来获取.txt 文件。我在下面尝试过
d = list(filter(lambda x : x != 'folder3', next(os.walk('.'))[1]))
但进一步的步骤无法弄清楚。如何将两者包含在一起以协同工作?
编辑:
我尝试将提供的链接引用为已回答的查询,但我无法在下面获得所需的输出,并且令人惊讶的是得到空列表作为 a 的输出
a=[]
for root, dirs, files in os.walk('.'):
dirs[:] = list(filter(lambda x : x != 'folder3', dirs))
for txt in glob(os.path.join(file[0], '*.txt')):
a.append(txt)
【问题讨论】:
-
它有点重复,但根据我的要求和上面的代码似乎不符合我的目的,但让我试试..
-
@Isma 您引用的链接对我的代码没有帮助。需要帮助。
-
a=[] for root, dirs, files in os.walk('.'): dirs[:] = list(filter(lambda x : x != 'folder3', dirs)) for pdf in glob(os.path.join(file[0], '*.txt')): a.append(txt)a提供空列表作为输出,但还有其他可用文件夹。 @伊斯玛 -
@Isma 我认为
for pdf in glob(os.path.join(file[0], '*.txt')需要修复,以避免挖掘folder3
标签: python python-3.x python-2.7 lambda