1

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

Community
  • 1
  • 1
Rohan Shah
  • 39
  • 1
  • 6
  • `atime` needs filesystem support to work. it seems like yours does not – Alex P. Nov 13 '14 at 07:37
  • On what OS are you working? –  Nov 13 '14 at 07: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? – Rohan Shah Nov 13 '14 at 07:53

2 Answers2

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)
AlvaroAV
  • 10,335
  • 12
  • 60
  • 91
  • 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? – Rohan Shah Nov 13 '14 at 08:24
  • How are you accessing the file ? – AlvaroAV Nov 13 '14 at 08:31
  • Using vi or subl . – Rohan Shah Nov 13 '14 at 08:33
  • 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 – AlvaroAV Nov 13 '14 at 08:45
  • Maybe this question could be useful to you: [Access time does not change after a file is opened](http://stackoverflow.com/questions/19551139/access-time-does-not-change-after-a-file-is-opened) – AlvaroAV Nov 13 '14 at 08:47
  • And also this question may help you to understand why is this happening, and why the atime is not being updated: [Why is not changing the access time?](http://superuser.com/a/464737) – AlvaroAV Nov 13 '14 at 08:51
  • Thank you so much Liarez. You've been of awesome help. I have relatime default mount option on my OS. Need to change it and try. – Rohan Shah Nov 13 '14 at 09:01
  • Take care with this kind of changes ! I'm happy my answer helped you – AlvaroAV Nov 13 '14 at 09:10
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