在当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出相对路径:
import os
testfiles = []
testfilepaths = []
L = len(os.path.abspath('.'))def searchfile(path):for item in os.listdir(path):if os.path.isdir(os.path.join(path, item)):searchfile(os.path.join(path, item))else:if 'test' in item:print(item, path[L:])searchfile(os.path.abspath('.'))