Python Forum
Check last time file was accessed
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check last time file was accessed
#1
Hello,
How do I check the time at which a file was opened last time ?
Thanks.
Reply
#2
Try using
https://docs.python.org/3/library/os.html#os.stat Wrote:os.stat(path, *, dir_fd=None, follow_symlinks=True)
Get the status of a file or a file descriptor. Perform the equivalent of a stat() system call on the given path. path may be specified as either a string or bytes – directly or indirectly through the PathLike interface – or as an open file descriptor. Return a stat_result object.
to get
https://docs.python.org/3/library/os.htm...t.st_atime Wrote:st_atime
Time of most recent access expressed in seconds.
Reply
#3
FYI: The same command is available if you are using pathlib objects see: https://docs.python.org/3/library/pathli....Path.stat

The command structure is the same (or very similar) as os.stat
Yoriz likes this post
Reply
#4
(Jun-01-2021, 01:10 PM)Yoriz Wrote: Try using
https://docs.python.org/3/library/os.html#os.stat Wrote:os.stat(path, *, dir_fd=None, follow_symlinks=True)
Get the status of a file or a file descriptor. Perform the equivalent of a stat() system call on the given path. path may be specified as either a string or bytes – directly or indirectly through the PathLike interface – or as an open file descriptor. Return a stat_result object.
to get
https://docs.python.org/3/library/os.htm...t.st_atime Wrote:st_atime
Time of most recent access expressed in seconds.
Thanks. Not clear how to use. For example, how can I get last access time to all files, located in 'aaa/bbb/ccc/' ?
Reply
#5
Try this with a loop through your files
import pathlib
import datetime

def get_file_access_datetime(file_path):
    path = pathlib.Path(file_path)
    path_stat = path.stat()
    return datetime.datetime.fromtimestamp(path_stat.st_atime)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  please check this i wanna use a csv file as a graph xCj11 5 1,499 Aug-25-2022, 08:19 PM
Last Post: deanhystad
  check if a file exist on the internet and get the size kucingkembar 6 1,768 Apr-16-2022, 05:09 PM
Last Post: kucingkembar
  Code to check folder and sub folders for new file and alert fioranosnake 2 1,945 Jan-06-2022, 05:03 PM
Last Post: deanhystad
  How to check if a file has finished being written leocsmith 2 7,849 Apr-14-2021, 04:21 PM
Last Post: perfringo
  find the header location in a .bin file without reading the whole file at a time SANJIB 0 2,222 Mar-05-2021, 04:08 PM
Last Post: SANJIB
  Naming the file as time and date. BettyTurnips 3 2,982 Jan-15-2021, 07:52 AM
Last Post: BettyTurnips
  how to change the range of read CSV file every time python file runs greenpine 6 4,470 Dec-08-2020, 10:11 PM
Last Post: greenpine
  Check if a file exists. Pedroski55 5 3,316 Sep-08-2020, 10:01 AM
Last Post: Pedroski55
  How to check to see a dbf file is EOF ? DarkCoder2020 0 1,727 Jun-16-2020, 05:03 PM
Last Post: DarkCoder2020
  Building a script to check size of file upon creation mightyn00b 2 2,386 Apr-04-2020, 04:39 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020