Discuss / Python / 练习2-在当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件

练习2-在当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件

Topic source
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os

def findKeyFiles(dirPath, keyStr):
    for f in os.listdir(dirPath):
        fpath = os.path.join(dirPath, f)
        if os.path.isdir(fpath):
            findKeyFiles(fpath, keyStr)
        else:
            fname = os.path.split(fpath)
            if keyStr in fname[1]:
                print(fpath)


dirPath = 'D:\\PyWorkplace\\'
findKeyFiles(dirPath, 'tr')

查找当前面目录:

findKeyFiles('.','test')
# 命令行传参
if __name__ == "__main__":
    findKeyFiles('.', sys.argv[1])

命令行输入

python findKeyFiles.py test
python findKeyFiles.py 总结

  • 1

Reply