Python 我如何转换 os.path.getctime()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19501711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:53:53  来源:igfitidea点击:

How can i convert os.path.getctime()

pythonos.path

提问by Iem-Prog

How can i convert os.path.getctime()to the right time?

我怎样才能转换os.path.getctime()到正确的时间?

My source code is :

我的源代码是:

import os

print("My Path: "+os.getcwd())
print(os.listdir("."))
print("Root/: ",os.listdir("/"))


for items in os.listdir("."):
    if os.path.isdir(items):
        print(items+" "+"Is a Directory")
        print("---Information:")
        print("    *Full Name: ",os.path.dirname(items))
        print("    *Created Time: ",os.path.getctime(items))
        print("    *Modified Time: ",os.path.getmtime(items))
        print("    *Size: ",os.path.getsize(items))
    else:
        print(items+" Is a File")

Output:

输出:

---Information:
    *Full Name:  
    *Created Time:  1382189138.4196026
    *Modified Time:  1382378167.9465308
    *Size:  4096

回答by Kristaps Taube

I assume that by right timeyou mean converting timestamp to something with more meaning to humans. If that is the case then this should work:

我认为,到了正确的时间,您的意思是将时间戳转换为对人类更有意义的东西。如果是这种情况,那么这应该有效:

>>> from datetime import datetime
>>> datetime.fromtimestamp(1382189138.4196026).strftime('%Y-%m-%d %H:%M:%S')
'2013-10-19 16:25:38'

回答by CDspace

From the documentation

文档

The return value is a number giving the number of seconds since the epoch (see the timemodule)

返回值是一个数字,给出自纪元以来的秒数(参见time模块)

And in the timemodule we see localtime()

time模块中我们看到localtime()

Use the following functions to convert between time representations:

...

| seconds since the epoch | struct_timein local time | localtime()|

使用以下函数在时间表示之间进行转换:

...

| 自纪元以来的秒数 | struct_time当地时间 | localtime()|

And from there use strftime()to get the format you want

并从那里用于strftime()获取您想要的格式

回答by Yang Wang

>>> from datetime import date
>>> date.fromtimestamp(path.getatime("/Users"))
    datetime.date(2015, 3, 10)