源代码:
#导入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']