0

I currently have a statement that says:

import glob
push_directory = 'Risk_Summary'

current_dir = os.getcwd() + '/Reports/' + push_directory + '/Saved_PDF/*'
list_of_files = glob.glob(current_dir)
latest_file = max(list_of_files, key=os.path.getctime)
file_name = os.path.basename(latest_file)
date_created = datetime.fromtimestamp(os.path.getctime(latest_file))

if date_created.day == datetime.now().day:
    risk_summary_report(latest_file, file_name, push_directory)
else:
    pass

The problem with using date created is the fact that it relies on the time someone created the file.

Here I have 2 options

  • Accept a range of dates
  • Figure how to get 'date accessed' which would result in the day it was saved.

How can I change my logic to either accept a week range of dates, or (preferably) how can I get the date of access?


I found this link: Python get last reading time of a file

Having trouble figuring out how to implement it in my code. Thank you very much!

sgerbhctim
  • 3,420
  • 7
  • 38
  • 60

1 Answers1

2

You can access the last accessed time by os.path.getatime().

From Python docs:

os.path.getatime(path)
Return the time of last access of path. The return value is a number giving the number of seconds since the epoch (see the time module). Raise OSError if the file does not exist or is inaccessible.

Sample Usage

os.path.getatime('./test.txt')

Output

1503332001.0
Community
  • 1
  • 1
tdube
  • 2,453
  • 2
  • 16
  • 25