1

Thsi question is edited with new information further below:


I am on Windows 7 Machine using Cygwin.. and I have both Python 2.6 and 3.1 installed.

I can see the following using my short python script.. for Created Time, Modified Time, Access Time and Created Time using stat.

But, the problem is that.. Windows 7 File Properties shows Created Time as 11/12/2010 11:57:54 AM.

My question is: How can I return the Windows Created Time in a python script.

I repeat I don't want to see the fctime as returned in the script below. It is NOT the same as Windows Created Time.

Please advise how I can do this.. and why is there a difference.. please explain.

Yes.. and I have read the documentation for os.stat.. and it says:

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

$ python /tmp/python/filemodified.py marksix.py
marksix.py
fctime: 11/12/2010 22:58:25
fmtime: 11/12/2010 22:57:01 (windows shows 22:57:01 ok)
fatime: 11/12/2010 22:45:21 (windows shows 22:45:21 ok)
fctimestat: Sat Dec 11 22:58:25 2010 (same as above fctime)
fsize: 1765

some what related to this posting: Get file creation time with Python on Mac

Here is my full script:

import sys
import os
import time

for f in sys.argv[1:]:
    if os.path.exists(f):

        fname = f
        fctime = time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(os.path.getctime(fname)))
        fmtime =  time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(os.path.getmtime(fname)))
        fatime =  time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(os.path.getatime(fname)))
        fsize = os.path.getsize(fname)
        #print "size = %0.1f kb" % float(fsize/1000.0)
        fctimestat = time.ctime(os.stat(fname).st_ctime)

        print fname + '\nfctime: ' + fctime + '\nfmtime: ' + fmtime + '\nfatime: ' + fatime + '\n',
        print 'fctimestat: ' + fctimestat + '\n',
        print 'fsize:', fsize,
        print

Additional Info:

Now.. I am in my work environment.. so I can't use the same file as before.. but anyway, I tested with cygwin python (2.6.5) and windows python (2.6.6).. and the results are different.. as can be seen below.

First one is cygwin Python.. and second one is Windows Python. And, Windows Python matches the File Properties... So, is this difference normal..? Why should cygwin python get the same set of dates as Windows..?

User@COMP /tmp/pythonscr
$ python file_time.py ../testjpg.gif
../testjpg.gif
fctime: 05/01/2011 10:25:52
fmtime: 05/01/2011 10:25:52
fatime: 01/12/2010 17:30:16
fctimestat: Wed Jan  5 10:25:52 2011
fsize: 1536
------


User@COMP /tmp/pythonscr
$ /cygdrive/c/Python26/python.exe file_time.py ../testjpg.gif
../testjpg.gif
fctime: 01/12/2010 17:30:16
fmtime: 05/01/2011 10:25:52
fatime: 01/12/2010 17:30:16
fctimestat: Wed Dec 01 17:30:16 2010
fsize: 1536
------
Community
  • 1
  • 1
ihightower
  • 3,093
  • 6
  • 34
  • 49
  • @nosklo: He's talking about a difference of less than a minute, does knowing the time zone help answer this question? – Steven Rumbalski Jan 04 '11 at 15:16
  • i don't know if it is of any help.. my time zone is UTC+7. – ihightower Jan 04 '11 at 15:21
  • On my Windows 7 machine *without Cygwin* your script gives accurate times. – Steven Rumbalski Jan 04 '11 at 15:31
  • On my Windows XP machine *without Cygwin* your script's output matches the system reported file's properties. – martineau Jan 04 '11 at 19:37
  • I have added additional comments to my original question.. please see the end of the question. Yes.. Now I see that without cygwin the system reported file's properties are matched as you guys. But, why would then cygwin report differenty.. Is this normal? and expected behaviour!! – ihightower Jan 05 '11 at 03:38
  • 1
    Cygwin probably tries to emulate the Unix behavior and therefore returns the change time, as documented. Unfortunately, since Posix does not have a creation time in `stat` (that I know of), you are out of luck. Try `ntpath.getctime` if you have that. – Emilio Silva Jan 05 '11 at 04:32

1 Answers1

1

I tried to do this once again at home.. using 1) cygwin python 2.6 and 2) windows python 3.1 on the same file.. and with both ntpath and os.path

The conclusion is that.. windows python 3.1 results match with Windows Properties for all 3 times.

For, cygwin python, the created time do not match with windows properties.. on using both ntpath and os.path... other ones are ok and matched.

Here are the results:

Cygwin Python 2.6.5 results

os.path

>>> time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(os.path.getctime(fname)))
'11/12/2010 22:58:25'
>>> time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(os.path.getmtime(fname)))
'11/12/2010 22:57:01'
>>> time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(os.path.getatime(fname)))
'11/12/2010 22:45:21'

ntpath

>>> time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(ntpath.getctime(fname)))
'11/12/2010 22:58:25'
>>> time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(ntpath.getmtime(fname)))
'11/12/2010 22:57:01'
>>> time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(ntpath.getatime(fname)))
'11/12/2010 22:45:21'

Windows Python 3.1 results

os.path

>>> time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(os.path.getctime(fname)))
'11/12/2010 11:57:54'
>>> time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(os.path.getmtime(fname)))
'11/12/2010 22:57:01'
>>> time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(os.path.getatime(fname)))
'11/12/2010 22:45:21'

ntpath

>>> time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(ntpath.getctime(fname)))
'11/12/2010 11:57:54'
>>> time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(ntpath.getmtime(fname)))
'11/12/2010 22:57:01'
>>> time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(ntpath.getatime(fname)))
'11/12/2010 22:45:21'
ihightower
  • 3,093
  • 6
  • 34
  • 49