Python查找指定文件

在当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出相对路径:

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('.'))
posted @ 2019-03-21 22:58  娘阔菜菜  阅读(7772)  评论(0编辑  收藏  举报