为什么这个循环不起作用

2024-10-05 10:01:45 发布

您现在位置:Python中文网/ 问答频道 /正文

def cut(path):
    test = str(foundfiles)
    newList = [s for s in test if test.endswith('.UnitTests.vbproj')]
    for m in newList:
        print m
    return newList

这个函数解析foundliles,foundliles是一个文件夹中的文件列表,我已经分析了大约20多个文件。我需要分析一下每一个文件的列表。单元测试.vbproj“但是,我不能让它工作。任何建议都将不胜感激!在

编辑1:这就是我现在编写代码的地方,我得到一个属性错误消息框,说明“tuple”对象没有属性“endswith”

^{pr2}$

Tags: 文件pathintest列表for属性def
2条回答

我真的不知道你的“基金档案”是什么类型的。 也许这样可以帮助你:

def cut(path):
    import os
    newlist = []
    for parent,dirnames,filenames in os.walk(path):
        for FileName in filenames:
            fileName = os.path.join(parent,FileName)
            if fileName.endswith('.UnitTests.vbproj'):newlist.append(fileName)
   return newlist

你把列表变成了一个字符串。在test上循环会给您单独的字符:

>>> foundfiles = ['foo', 'bar']
>>> for c in str(foundfiles):
...     print c
... 
[
'
f
o
o
'
,

'
b
a
r
'
]

不需要将foundfiles转换为字符串。您还需要测试列表的元素,而不是test

^{pr2}$

相关问题 更多 >

    热门问题