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('.'))


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM