2

I created a simple text file and checked its timestamps using stat and here is the output:

stat Output before any python script

papagolf@Sierra ~/My Files/My Programs/Learning/Python % stat prac09_timestamp.txt 
  File: prac09_timestamp.txt
  Size: 10          Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d  Inode: 2485542     Links: 1
Access: (0600/-rw-------)  Uid: ( 1000/papagolf)   Gid: ( 1001/papagolf)
Access: 2017-09-10 11:10:40.979845492 +0530
Modify: 2017-09-10 11:10:40.979845492 +0530
Change: 2017-09-10 11:10:40.979845492 +0530
 Birth: -

Then I ran a python script that simply opens the file in read mode. I was expecting the time "Access" stamp to change but it didn't.

Python Script and output of stat after running it

papagolf@Sierra ~/My Files/My Programs/Learning/Python % cat python prac09_timestamp.py 
cat: python: No such file or directory
a = open("prac09_timestamp.txt", "r")
a.close()
papagolf@Sierra ~/My Files/My Programs/Learning/Python % python prac09_timestamp.py 
papagolf@Sierra ~/My Files/My Programs/Learning/Python % stat prac09_timestamp.txt 
  File: prac09_timestamp.txt
  Size: 10          Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d  Inode: 2485542     Links: 1
Access: (0600/-rw-------)  Uid: ( 1000/papagolf)   Gid: ( 1001/papagolf)
Access: 2017-09-10 11:10:40.979845492 +0530
Modify: 2017-09-10 11:10:40.979845492 +0530
Change: 2017-09-10 11:10:40.979845492 +0530
 Birth: -

But when I change the script to actually read the data, the time stamp changed. Can you explain it?

New Python Script and output of stat after running it

papagolf@Sierra ~/My Files/My Programs/Learning/Python % cat prac09_timestamp.py 
a = open("prac09_timestamp.txt", "r")
a.readlines()
a.close()
papagolf@Sierra ~/My Files/My Programs/Learning/Python % python prac09_timestamp.py
papagolf@Sierra ~/My Files/My Programs/Learning/Python % stat prac09_timestamp.txt 
  File: prac09_timestamp.txt
  Size: 10          Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d  Inode: 2485542     Links: 1
Access: (0600/-rw-------)  Uid: ( 1000/papagolf)   Gid: ( 1001/papagolf)
Access: 2017-09-10 11:13:12.699848321 +0530
Modify: 2017-09-10 11:10:40.979845492 +0530
Change: 2017-09-10 11:10:40.979845492 +0530
 Birth: -
  • 2
    I suppose the simple answer is that Python opening the file doesn't actually access the file. Which isn't really surprising. If you want to know what Python is doing when it opens the file, I guess you'd have to ask on a Python forum or look at the source code. – Faheem Mitha Sep 10 '17 at 6:09
5

I don't think this is a Python question at all. It's system level. And at that level opening a file is an entirely distinct operation from reading a file. I'm not an expert by any stretch of the imagination on Linux internals but here are some basic facts:

  • A file is opened with the open(2) system call.
  • A file is read with the read(2) system call
  • There is a flag for open(2) that instructs the system to not update access time when read(2) is called. It's called O_NOATIME
  • You can infer from this that open(2) doesn't update access time

If you think about what the word access means this makes sense. open(2) returns a file descriptor that can subsequently be used to read the file. To use a crude analogy, it's like having an all-access pass to a concert. Until you show up and start hitting on groupies back stage you haven't accessed anything. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.