Discuss / Python / 只能输出所有包含某字符串的文件/目录的绝对路径 相对路径不会搞。非当前目录不会搞!

只能输出所有包含某字符串的文件/目录的绝对路径 相对路径不会搞。非当前目录不会搞!

Topic source

-_coding:utf-8 --

import os

def findStr(p, w):

for x in os.listdir(p):
    p1 = os.path.join(os.path.abspath(p),x)
    if w in x:
        print(p1)

    if os.path.isdir(p1):
        findStr(p1, w)

findStr(os.path.abspath('.'),'test')


  • 1

Reply