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.
3 Answers
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
Ruchir Shukla
No, it is not the proper solution, as getctime is returning the last Change time. and not Creation time
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
Ruchir Shukla
No, it is not the proper solution, as st_ctime is returning the last Change time. and not Creation time
Ishan Khare
@RuchirShukla that's what the documentation defines it does, on unix systems - docs.python.org/2/library/stat.html#stat.ST_CTIME
Ruchir Shukla
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)."