我的解答
import os
def findFile(path,key, rootPath): if os.path.isdir(path): for item in os.listdir(path): findFile(os.path.join(path,item), key,rootPath) elif os.path.isfile(path): fileName = os.path.split(path)[-1] if fileName.find(key) != -1: print("The absolute path is:", path) print("The relative path is:", os.path.relpath(path, rootPath))
rootPath = "." //可以是指定的路径 key = "xxxx" //指定的关键字 findFile(rootPath, key, rootPath)
Sign in to make a reply
lubang03
我的解答
import os
def findFile(path,key, rootPath): if os.path.isdir(path): for item in os.listdir(path): findFile(os.path.join(path,item), key,rootPath) elif os.path.isfile(path): fileName = os.path.split(path)[-1] if fileName.find(key) != -1: print("The absolute path is:", path) print("The relative path is:", os.path.relpath(path, rootPath))
rootPath = "." //可以是指定的路径 key = "xxxx" //指定的关键字 findFile(rootPath, key, rootPath)