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
How can i convert os.path.getctime()
提问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
time
module)
返回值是一个数字,给出自纪元以来的秒数(参见
time
模块)
And in the time
module we see localtime()
在time
模块中我们看到localtime()
Use the following functions to convert between time representations:
...
| seconds since the epoch |
struct_time
in 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)