0

I have a list of dates and times(from sar in this case) and a list of files in a directory. I want to go through the list of dates/times from list A and for each date/time listed, list the file that was modified at that time. My current idea is to implement two for loops, so for every item in list A do another for loop to look through the directory and test if item in A matches the files modified date.

Unfortunately This would be very inefficient and I'm not sure if it will even work. The lists are in datetime.datetime() format

List A is just dates List B is the modified dates pulled from the files in the directory(using glob)

Any thoughts?

Pablo Stark
  • 682
  • 10
  • 34
user1601716
  • 1,893
  • 4
  • 24
  • 53

1 Answers1

2

Instead of making B as a list make it a dictionary.

B = {}
import os
import datetime
def modification_date_time(file_name):
    time = os.path.getmtime(file_name)
    return datetime.datetime.fromtimestamp(time)

for file in file_list:
    B[modification_date_time(file_name)] = file_name

From which you can get the modified file directly without any search.
P.S As you have mentioned it as "file" and not "files" each modified date time will have only one file.

sachin irukula
  • 12,841
  • 4
  • 19
  • 19