2

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?

1
  • By the way, since Python 2.2 you can access the items in the return value more easily as attributes, thus os.stat(r'path').st_ctime Commented Jan 18, 2010 at 18:24

4 Answers 4

10

Why not?

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

Looks like a valid creation time to me.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 Short and elegant, if you're interested in the textual representation.
5

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().

1 Comment

Perhaps os.stat('/tmp/myfile.txt').st_ctime would be better than os.stat('/tmp/myfile.txt')[9], as long as python ≥2.2
3

Are you sure it isn't? Using unixtimestamp.com it converts to "01/18/2010 @ 7:34am" which at least makes sense.

The timestamp is returned as a time in seconds from 1970-01-01.

Comments

0

See http://docs.python.org/library/os.html#os.stat

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

Everything is OK.

1 Comment

I think he needs the conversion between the integer and a more readable time format.

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.