1

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!

Community
  • 1
  • 1
  • I think this is a similar case: http://stackoverflow.com/a/237092/3707140 – rebelity Jun 25 '14 at 13:53
  • On some systems (like Unix) `getctime()` will return the time of the _last change_. And on windows you will get the _time of creation_. I guess you are on a *NIX machine, right? – Colin O'Coal Jun 25 '14 at 13:53

2 Answers2

1

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.

Community
  • 1
  • 1
Christian Berendt
  • 3,416
  • 2
  • 13
  • 22
0

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.

Mattias Backman
  • 927
  • 12
  • 25
  • ...but in my case "the time of creation" is not the actual time of creation of the file when it is copied. it is the time of creation of the copy. the date attribute has the actual time of creation of the file. – sebtac Dec 31 '21 at 01:26