1

The title says it all. I am trying to get the year a file was created for indexing purposes. Also, I'm on windows if that matters.

0

3 Answers 3

3

To be honest there are similar questions e.g. here

os.stat is your friend. It provides various stats on a file. Use ctime to change it into something human readable as demo'd here

import os.path, time when = os.stat(r"path").st_ctime time.ctime(when) 
Sign up to request clarification or add additional context in comments.

Comments

1

this question has been already asked, short story here, see the code below:

import os.path, time print "last modified: %s" % time.ctime(os.path.getmtime(file)) print "created: %s" % time.ctime(os.path.getctime(file)) 

and here is link: How to get file creation & modification date/times in Python?

1 Comment

No, it is not the proper solution, as getctime is returning the last Change time. and not Creation time
0

so here's the solution:

import os import datetime #gives the create time as a unix timestamp create_time = os.stat(<path to file>).st_ctime #returns a datetime object create_datetime = datetime.datetime.fromtimestamp(create_time) #print the year create_datetime.strftime("%Y") 

3 Comments

No, it is not the proper solution, as st_ctime is returning the last Change time. and not Creation time
@RuchirShukla that's what the documentation defines it does, on unix systems - docs.python.org/2/library/stat.html#stat.ST_CTIME
That's exactly what I am saying."On some systems (like Unix) is the time of the last metadata change, and, on others (like Windows), is the creation time (see platform documentation for details)."

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.