首先自定义一个函数,以后只要调用这个函数,就能返回年月日和调用的时间
def logger():
#返回年月日和调用的时间
import time
time_format='%Y-%m-%d %X '
time_current= time.strftime(time_format )
return time_current
print(logger())
with open('a.txt','a+',encoding= 'utf-8') as f:
f.write('{times} write next'.format(times= logger() ) )
---------------------------------------------------------------------
%X(注意是大写的X)相当于%H:%M:%S;想要输出年月日的格式,只要把logger中的 “-” 直接换成年月日