我学习的Python(对win8的2.7.9版本)和目前正通过各种date
和time
模块。我无法使用来获取文件的最后修改时间ctime
。我遇到这个错误:
AttributeError: type object 'datetime.time' has no attribute 'ctime'
这是我的进口商品:
import os
from os import path
from datetime import date,time, timedelta
from datetime import datetime
脚本:
modTime = time.ctime(os.path.getmtime("t.txt"))
print "t.txt was last modified at: " + modTime # This Doesn't work
print datetime.fromtimestamp(path.getmtime("t.txt")) # This works
错误消息非常清楚:datetime.time has no attribute 'ctime'
。但是time
模块有功能ctime
。您正在time
用from datetime import time
直线遮住模块。
>>> import time
>>> time # refers to the *module*
<module 'time' from '/usr/lib/python2.7/lib-dynload/time.so'>
>>> time.ctime()
'Sun Feb 1 16:23:33 2015'
>>> from datetime import time
>>> time # now we have a class of that name
<type 'datetime.time'>
>>> t = time()
>>> t.isoformat()
'00:00:00'
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句