2

I am trying to get the last access file using Python. Even after accessing the files using vi/sublime or any other editor, the access time of the file does not get updated. I have tried to use the function os.stat(full_path).st_atime but of no use. It throws out the correct result only if the file is modified.

Just following up on the link alternative to getatime to find last file access in python

3
  • atime needs filesystem support to work. it seems like yours does not Commented Nov 13, 2014 at 7:37
  • On what OS are you working? Commented Nov 13, 2014 at 7:37
  • I am working on Linux (Ubuntu). I was able to find the solution using find command in Linux: find /home/dir/ -atime -10 -print. But, I want use this command using Python. Is there a way around this? Commented Nov 13, 2014 at 7:53

2 Answers 2

2

You should check this way:

(mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(file)
print "last access: %s" % time.ctime(atime)

I recommend you to check official info at os.stat() documentation:

To check creation&modification dates

print "last modified: %s" % time.ctime(os.path.getmtime(file))
print "created: %s" % time.ctime(os.path.getctime(file))

OR

(mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(file)
print "Modification date: %s" % time.ctime(mtime)
print "Creation date: %s" % time.ctime(ctime)
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks Liarez for answering this, but still when I access the file recently, the atime parameter does not get updated and reveals the old timestamp.The os.stat() documentation reveals that st_atime has only 1-day resolution. What does it mean?
How are you accessing the file ?
Using vi <filename> or subl <filename>.
Not all the commands modify the access tiem of a file. If you want to change the access time of a file you can use touch -a filename and then if you check the atime it will be updated. I'm not sure why vi isn't modifying the atime
Maybe this question could be useful to you: Access time does not change after a file is opened
|
0

Find last creation file:

def findlatestfile(folder):
    list_of_files = glob.glob(folder+'/*') 
    latest_file = max(list_of_files, key=os.path.getctime)
    return latest_file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.