搜寻路径下的所有文件,输出文件名包含特定字符串的绝对文件路径

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36528804/article/details/82532318

python实现:在当前目录(absPath)以及当前目录(absPath)的所有子目录下查找文件名包含指定字符串findStr的文件,并打印出绝对路径。
代码如下:

import os

def f(absPath, findStr):
    for fileName in os.listdir(absPath):
        fileAbsPath = os.path.join(absPath, fileName)

        if os.path.isdir(fileAbsPath):
            f(fileAbsPath, findStr)

        if os.path.isfile(fileAbsPath):
            if findStr in fileName:
                print(fileAbsPath)

absPath = '目标绝对路径'
findStr = '所寻字符串'
f(absPath, findStr)

猜你喜欢

转载自blog.csdn.net/qq_36528804/article/details/82532318