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

发布于 2024-10-10 10:16:05 字数 2584 浏览 0 评论 0原文


这个问题是用下面进一步的新信息编辑的:


我在使用 Cygwin 的 Windows 7 机器上..并且我安装了 Python 2.6 和 3.1。

我可以使用我的简短 python 脚本看到以下内容..使用 stat 查看创建时间、修改时间、访问时间和创建时间。

但是,问题是......Windows 7 文件属性显示创建时间为 11/12/2010 11:57:54 AM

我的问题是: 如何在 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
------

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若无相欠,怎会相见 2024-10-17 10:16:05

我尝试在家里再次执行此操作..使用 1) cygwin python 2.6 和 2) windows python 3.1
在同一个文件上.. 并使用 ntpath 和 os.path

结论是.. windows python 3.1 结果与 Windows 属性匹配 3 次。

对于 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'

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'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文