python 查找文件,循環遍歷文件夾,獲取文件夾中的某個文件中的文件,判斷文件是否包含在某個文件夾下



import os

filepath = r'C:\Users\bqm66\AppData\Local\Encoo\Encoo Robot\JobLogs\local\管理員權限可以訪問的文件\1.0.0'
filelist = []


# 拿到所有的日志文件
class LogFile:
    try:
        pathDir = os.listdir(filepath)
        for allDir in pathDir:
            child = os.path.join('%s%s' % (filepath, allDir))
            # print('打印文件', child)
            filelist.append(child)
            # print('所有文件', filelist)
        '''
        1.拿到最后一個(即最新的執行時間的文件)
        2.需要做字符串取值,拿到后14個字符
        3.做字符串拼接,獲取到真正正確的路徑,可輸入文件位置直接打開的文件有效路徑
        4.校驗log文件是否存在
        5.校驗截圖文件是否存在,獲取文件中的截圖數量,打印輸出結果
        '''
        endfile_name = filelist[-1]
        # print(type(endfile_name), endfile_name)
        # print('拿到后14個字符', endfile_name[-14:])
        new_file = endfile_name[-14:]
        rod = f'\{new_file}'
        # 我是最后轉換的正確路徑
        correct_path = filepath + rod
        # print('我是最后轉換的正確路徑', correct_path)
        # 進行文件夾內容循環獲取
        pathDir = os.listdir(correct_path)
        for allDir in pathDir:
            child = os.path.join('%s%s' % (filepath, allDir))
            # print('打印正確的文件', type(child), child)
            correctlogname = 'job-' + new_file + '.log'
            jietu = 'Screenshots'
            if correctlogname and jietu in child:
                print('日志文件已存在,Screenshots文件已存在')
        # 截圖路徑
        jietu_road = '\Screenshots'
        correct_jietu_road = correct_path + jietu_road
        # print('我是最后轉換的正確路徑', correct_jietu_road)
        pathDir = os.listdir(correct_jietu_road)
        child_list = []
        for allDir in pathDir:
            child = os.path.join('%s%s' % (filepath, allDir))
            # print('打印正確的文件', type(child), child)
            child_list.append(child)
            jietu_num = len(child_list)
            # print('我最后的值是', len(child_list), child_list)
            if 'jpg' in child:
                print(f'執行截圖功能通過,總共有{jietu_num}張截圖')
    except:
        print('系統資源文件不匹配,非有效文件路徑,點擊Robot日志,檢查對應磁盤文件配置')

LogFile()


免責聲明!

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



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