在Python中:如何返回文件的“Windows”创建时间?

1 投票
1 回答
3085 浏览
提问于 2025-04-16 09:23

这个问题下面有更新的信息:


我在使用Cygwin的Windows 7电脑上,安装了Python 2.6和3.1。

我用一个简单的Python脚本查看了文件的创建时间、修改时间、访问时间和创建时间。

但是,问题是,Windows 7的文件属性显示的创建时间是2010年11月12日 上午11:57:54

我的问题是: 我该如何在Python脚本中获取Windows的创建时间。

我强调一下,我不想看到下面脚本中返回的fctime,因为它和Windows的创建时间不一样。

请告诉我该怎么做……还有为什么会有差异,请解释一下。

是的,我已经阅读了os.stat的文档……上面说:

st_ctime(平台相关;在Unix上是最近一次元数据更改的时间,在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

这和这个帖子有点关系:在Mac上用Python获取文件创建时间

这是我的完整脚本:

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

附加信息:

现在……我在工作环境中,所以不能使用之前的文件……不过,我用Cygwin的Python(2.6.5)和Windows的Python(2.6.6)进行了测试,结果是不同的……如下所示。

第一个是Cygwin的Python,第二个是Windows的Python。而且,Windows的Python结果和文件属性一致……那么,这种差异正常吗?为什么Cygwin的Python获取的日期和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
------

1 个回答

1

我在家里又试了一次.. 使用了 1) cygwin 的 Python 2.6 和 2) Windows 的 Python 3.1,都是在同一个文件上进行的.. 而且用了 ntpath 和 os.path 两种方式。

结论是.. Windows 的 Python 3.1 的结果和 Windows 属性完全一致,三次测试都是这样。

但是,使用 cygwin 的 Python 时,创建时间和 Windows 属性不一致.. 不管是用 ntpath 还是 os.path,其他的结果都正常且一致。

以下是结果:

Cygwin Python 2.6.5 的结果

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 的结果

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'

撰写回答