如何打开与子目录中的模式匹配的文件

ste*_*teT 3 python

您好我在子目录中查找和打开文件时遇到问题.我有几个不同的文件,例如:
mouse_1_animal.txt
mouse_2_animal.txt mouse_3_animal.txt

所以我想在工作目录的子目录中找到所有这些文件并打开它们并使用那些行做一些事情.这是我的尝试:

i=1
for path, subdirs, files in os.walk(root) :
    for file in files :
        if file == "mouse_{0}_animal.txt".format(i) :
            #do something
            i = i + 1
Run Code Online (Sandbox Code Playgroud)

但显然它没有找到所有的文件,所以我想知道这是否是我用来找到错误的文件的方式.

haa*_*vee 8

pythonic方式:

import glob
for f in glob.glob('./subDir/mouse_*_animal.txt'):
    # do_something
Run Code Online (Sandbox Code Playgroud)