【问题标题】:Windows file creation date/time using python使用 python 创建 Windows 文件的日期/时间
【发布时间】:2011-01-06 08:36:07
【问题描述】:

我需要使用 python 获取文件创建日期和时间。我试过了:

os.stat(r"path")[ST_CTIME]

但它正在返回:

1263538277

这不是创建日期时间。有办法吗?

【问题讨论】:

  • 顺便说一下,从 Python 2.2 开始,您可以更轻松地访问返回值中的项目作为属性,因此 os.stat(r'path').st_ctime

标签: python datetime


【解决方案1】:

为什么不呢?

>>> import time
>>> time.ctime(1263538277)
'Fri Jan 15 04:51:17 2010'

在我看来是一个有效的创建时间。

【讨论】:

  • +1 简短而优雅,如果您对文本表示感兴趣。
【解决方案2】:

你确定不是吗?使用 unixtimestamp.com 它转换为“01/18/2010 @ 7:34am”,这至少是有意义的。

时间戳以从 1970-01-01 开始的秒数形式返回。

【讨论】:

    【解决方案3】:

    http://docs.python.org/library/os.html#os.stat

    st_ctime(取决于平台;Unix 上最近元数据更改的时间,或 Windows 上的创建时间)

    一切正常。

    【讨论】:

    • 我认为他需要整数和更易读的时间格式之间的转换。
    【解决方案4】:

    来自bytes.com

    import os
    import time
    create_date = os.stat('/tmp/myfile.txt')[9]
    print time.strftime("%Y-%m-%d", time.gmtime(create_date))
    

    这给出了:

    2009-11-25
    

    你也可以试试:

    print time.gmtime(create_date)
    (2009, 11, 25, 13, 37, 9, 2, 329, 0)
    

    为了更准确的时间戳。

    注意time.gmtime()返回的时间返回GMT;其他功能见the time module documentation,如localtime()

    【讨论】:

    • 或许os.stat('/tmp/myfile.txt').st_ctime会比os.stat('/tmp/myfile.txt')[9]好,只要python≥2.2
    猜你喜欢
    • 1970-01-01
    • 2011-07-11
    • 1970-01-01
    • 2015-10-09
    • 1970-01-01
    • 2011-10-16
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    相关资源
    最近更新 更多