Pythonos.步行()未搜索所有文件类型

2024-10-01 15:43:50 发布

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

我试图遍历一个目录并使用os.walk列出所有文件。我的代码是:

import sys
import os.path

def sourcedirs():
    print "Searching directory..."
    for dpath, dirs, files in os.walk(buildDir, followlinks=True):
        print files
        print len(files)
        for fileList in files:
            print fileList
            if '.cpp' in fileList:
                print fileList
            else:
                print "source files not found"
                sys.exit(1)

buildDir = os.getcwd()
print buildDir
sourcedirs()

首先,当我print fileList以列表的形式获取目录中的所有文件,如下所示。你知道吗

['test.py', 'template', 'SConsGenerator_version2.py', 'template-ORIGINAL-OLD', 'test.py~', 'SConsGenerator_version3.py~', 'SConsGenerator_version1.py', 'template-old', 'decomposePar.cpp', 'SConsscript', 'SConsGenerator_version3.py', 'template-ORIGINAL-NEW']

但是,紧跟在第二个for循环(for fileList in files:)之后的print fileList只打印

test.py

其余的文件没有显示。这看起来很琐碎,但我不知道这里发生了什么。你知道吗


Tags: 文件inpytestimport目录foros

热门问题