写日志的办法多种多样,我这个是我喜欢的办法,可以做个参考
没啥说的,直接上代码
import time def write_log(value): now_time = time.time() #获取当前日期和时间 time_format = ‘%Y-%m-%d %H:%M:%S‘ #指定日期和时间格式 time_put = time.strftime(time_format,now_time) #格式化时间,时间变成YYYY-MM-DD HH:MI:SS file_name = ‘/log/log.log‘ log_file = open(file_name,‘a‘) #这里用追加模式,如果文件不存在的话会自动创建 write_value = ‘%s %s‘ %(now_time, value) log_file.write(write_value) log_file.close() if __name__ == ‘__main__‘: value = ‘insert something into log_file‘ write_log(value)
原文地址:https://www.cnblogs.com/xiu123/p/8439215.html
时间: 2024-10-17 22:12:54