Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows file creation date/time using python

I need to get a file creation date&time using python. I tried:

os.stat(r"path")[ST_CTIME]

But it is returning:

1263538277

This is not the creation date time. Is there a way to do it?

like image 646
Vicky Avatar asked Jul 11 '26 00:07

Vicky


2 Answers

Why not?

>>> import time
>>> time.ctime(1263538277)
'Fri Jan 15 04:51:17 2010'

Looks like a valid creation time to me.

like image 83
Juliano Avatar answered Jul 13 '26 12:07

Juliano


From bytes.com:

import os
import time
create_date = os.stat('/tmp/myfile.txt')[9]
print time.strftime("%Y-%m-%d", time.gmtime(create_date))

Which gives:

2009-11-25

You can also try:

print time.gmtime(create_date)
(2009, 11, 25, 13, 37, 9, 2, 329, 0)

For a more accurate timestamp.

Note that the time returned by time.gmtime() returns GMT; See the time module documentation for other functions, like localtime().

like image 22
Adam Matan Avatar answered Jul 13 '26 14:07

Adam Matan