alec_djinn alec_djinn - 7 months ago 17
Python Question

File creation time do not match when using os.stat()

I need to access the date of creation of a file using Python.
As suggested in many posts, I am using

os.stat(filename)
to get such information.
However, the result I get is different from the one I expect.
Here an example:

import os, time
f = 'untitled.ipynb' # Created 30 March 2016 at 15:45
fileStats = os.stat(f)
time.ctime(fileStats.st_ctime)


The result is :

'Mon May 2 16:04:27 2016'


Which is incorrect, if I look for it using "Get Info" (in OSX), I get:

"Created: 30 March 2016 at 15:45"


Does anyone experience the same error?

Do you have any solution to suggest?

Answer

accordint to the documentation at https://docs.python.org/2/library/os.html:
since you are using OSX, what you need is st_birthtime instead of st_ctime

st_ctime - platform dependent; time of most recent metadata change on Unix, or the time of creation on Windows.

On other Unix systems (such as FreeBSD), the following attributes may be available (but may be only filled out if root tries to use them):

st_birthtime - time of file creation