import time def logger(): time_format = '%Y-%m-%d %X' #时间的一个格式 time_current = time.strftime(time_format) #strftime()函数接收以时间元组,并返回以可读字符串表示的当地时间 with open('a.txt','a+') as f: f.write('%s end action ' %time_current) def test1(): print('in the test1') logger() def test2(): print('in the test2') logger() def test3(): print('in the test3') logger() test1() test2() test3()
结果:
2018-04-25 09:06:18 end action 2018-04-25 09:06:18 end action 2018-04-25 09:06:18 end action