2013-10-21 174 views
13

我如何将os.path.getctime()转换为正确的时间?我如何转换os.path.getctime()

我的源代码是:

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

输出:

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

不清楚你的要求;你能在正确的时间澄清你的意思吗? – UpAndAdam

回答

26

我认为通过正确的时间你的意思是转换时间戳的东西有更多的意义给人类。如果是这样的话,那么这应该工作:

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

适合我... –

2

documentation

返回的值是一个数字,因为时代赋予的秒数(见time模块)

而在time模块中我们看到localtime()

使用下列功能时之间转换:

...

|自epoch |以来的秒数本地时间内的struct_time | localtime() |

从那里使用strftime()得到你想要的格式

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