Reputation: 13
I'm using Python on Windows7 to parse new names for records we have...My goal is to include a date token as "..._YYYYMMDD.pdf" as described below.
From looking at the files in windows explorer 'details' view, I have confirmed that the following gives me the "Date Modified."
Since I'm working with copies of the original files, the returned value is also the same as the "date created."
import time
print "created: %s" % time.ctime(os.path.getctime('23614 TO 23814 DOWNSTREAM.pdf'))
which returns
>>>created: Tue Jun 24 13:19:12 2014
HOWEVER...
I need the "Date" attribute (the oldest time stamp on the file) since this matches the TRUE date of creation of the report. Any idea how to get the 'date' attribute I'm looking for and not the "date created" and not "date modified"?
I'm too new to the forum to answer my own question, but here it is:
Thanks to Christian for sending me in the right direction. The following gives me the right date:
print "created: %s" % time.ctime(os.stat('23614 TO 23814 DOWNSTREAM.pdf')[-2])
'created: Mon Oct 31 13:51:14 2011'
as opposed to the following which is the date the copy was created:
print "created: %s" % time.ctime(os.path.getctime('23614 TO 23814 DOWNSTREAM.pdf'))
created: Tue Jun 24 13:19:12 2014
Now I just have to figure out how to format it the right way!
Upvotes: 1
Views: 1219
Reputation: 3556
You have to use the birth time of the file. The birth time needs to be supported by the used filesystem, for example ext4. Then you can use os.stat(path) to get the birth time of a specific file.
You can use stat
to check if the birth time is accessible on your file system. If the birth time field is empty have a look at this post.
Upvotes: 1
Reputation: 955
Do you intend to run it on Windows or a Unix system? You will rely on an underlying call to the OS and behavior is different.
On Windows, I believe that you will get the time of creation with your code.
Upvotes: 0