开发者

In Python: How to Return "Windows" Created Time of a File?


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
------


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'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜