python如何遍歷指定路徑下所有檔案(按按照時間區間檢索)
阿新 • • 發佈:2020-09-15
需求
要求 查詢資料夾裡某個日期區間內的word文件,全部word的名稱和路徑列出來,比如 7月5號到7月31號 D盤下的所有word文件。
修改檔案型別
修改檔案路徑
檢索檔案修改時間區間
#conding=utf8 import os import time g = os.walk(r"F:\學習資料\week_home") def judge_time_file(path,file,update_time): if not file.endswith(('.doc','.docx')): return False start_time = time.mktime(time.strptime('2020-04-12 00:00:00',"%Y-%m-%d %H:%M:%S")) end_time = time.mktime(time.strptime('2020-05-23 00:00:00',"%Y-%m-%d %H:%M:%S")) # print(start_time,update_time,end_time) if start_time < update_time < end_time: return True return False data_list = [] for path,dir_list,file_list in g: for file_name in file_list: local_time = os.stat(os.path.join(path,file_name)).st_mtime if judge_time_file(path,file_name,local_time): data_list.append([os.path.join(path,file_name),time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(local_time))]) data_list.sort(key=lambda x:x[1]) print(*data_list,sep='\n')
總結
到此這篇關於python如何遍歷指定路徑下所有檔案(按按照時間區間檢索)的文章就介紹到這了,更多相關python遍歷指定路徑下所有檔案內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!