其他分享
首页 > 其他分享> > 查找特定名称文件

查找特定名称文件

作者:互联网

源代码:

#导入OS模块
import os
#待搜索的目录路径
path = "Day1-homework"
#待搜索的名称
filename = "2020"
#定义保存结果的数组
result = []

def findfiles():
    #在这里写下您的查找文件代码吧!

    t = 1
    for root, dirs, files in os.walk(path):
        for filename in files:
            if "2020" in filename:
                result.append([t, root + "/" + filename])
                t += 1
    
    for i in result:
        print(i)


    

if __name__ == '__main__':
    findfiles()

运行结果:

[1, 'Day1-homework/26/26/new2020.txt']
[2, 'Day1-homework/4/22/04:22:2020.txt']
[3, 'Day1-homework/18/182020.doc']

标签:__,filename,查找,2020,名称,Day1,特定,homework,result
来源: https://blog.csdn.net/qq_45986917/article/details/122658482