简体   繁体   English

Python以毫秒为单位指定文件创建时间

[英]Python specify file creation time by milliseconds

After some research I couldn't find any solution to this so here it goes: 经过一番研究之后,我找不到任何解决方案,因此请按以下步骤进行:

Python's command: Python的命令:

time.ctime(os.path.getctime('/path'))

displays output as (eg): 将输出显示为(例如):

Fri Dec 2 16:06:05 2016 . Fri Dec 2 16:06:05 2016

How can I make it display not only hours/minutes/seconds but also milliseconds? 如何使它不仅显示小时/分钟/秒,还显示毫秒?

Use os.stat("/").st_ctime_ns gain nanoseconds level 使用os.stat("/").st_ctime_ns获得纳秒级

import os
import datetime

datetime.datetime.fromtimestamp(os.stat("/").st_ctime_ns)

You can use stat : 您可以使用stat

import datetime
import os

datetime.datetime.fromtimestamp(os.stat('/').st_ctime)

Note that if you modified the metadata of the file, you will get this as the "ctime", getting the creation date after modification of metadata is not possible on UNIX platforms. 请注意,如果您修改了文件的元数据,则将其称为“ ctime”,在UNIX平台上无法获得元数据修改后的创建日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM