Python traverses all files under the specified path and retrieves them according to the time interval

demand

It is required to find the word documents in a certain date range in the folder, and list the names and paths of all words, such as all word documents under the D drive from July 5 to July 31.

Modify file type

Insert picture description here

Modify file path

Insert picture description here

Retrieve file modification time interval

Insert picture description here

 

#conding=utf8
import os
import time
g = os.walk(r"C:\Users\Administrator\Downloads")
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 True
 
data_list = []
i = 0
 
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))])
            # print(data_list)
        i=i+1
        print(i)
data_list.sort(key=lambd

 

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *