将文本追加到带有时间戳的文本文件
问题描述:
我想将python代码的输出追加到文本文件中。以上是我的Python代码,我每2小时循环它将文本追加到带有时间戳的文本文件
if response == 0:
print(hostname, "is up")
if option == 1:
print(option, "is the option')
print('this is option number 1')
elif option == 2:
print(option, "is the option')
print('this is option number 2')
else:
print(option, "is the other option')
print('this is the result of other option')
我注意到我需要下面的代码将结果追加到文本文件。
with open("test.txt", "a") as myfile:
myfile.write("appended text")
如何将每个输出记录到文本文件并包含时间戳?例如
09:10 192.168.0.1 is up
09:10 1 is the option
09:11 this is option number 1
11:15 192.168.0.1 is up
11:10 1 is the option
11:11 this is option number 1
13:10 192.168.0.1 is up
13:10 3 is the other option
13:11 this is the result of other option
答
Python中确实有一个日志库,你可以使用的,但如果你希望让自己的,那么你可以采取以下方法。
这里有两个函数,write_log()
它接受你正在使用的参数并创建一些日志条目。然后,这个函数调用write_logline()
每个部分写入到两个屏幕(可选),也与时间戳文件包括:
from datetime import datetime
def write_logline(logfile, text):
now = datetime.strftime(datetime.now(), '%H:%M')
log_text = '{} {}\n'.format(now, text)
print(log_text, end='') # also display log info, comment out if not needed
logfile.write(log_text)
def write_log(response, hostname, option):
with open("test.txt", "a") as logfile:
if response == 0:
write_logline(logfile, '{} is up'.format(hostname))
if option == 1:
write_logline(logfile, '{} is the option'.format(option))
write_logline(logfile, 'this is option number 1')
elif option == 2:
write_logline(logfile, '{} is the option'.format(option))
write_logline(logfile, 'this is option number 2')
else:
write_logline(logfile, '{} is the other option'.format(option))
write_logline(logfile, 'this is the result of other option')
write_log(0, '192.168.0.1', 1)
write_log(0, '192.168.0.1', 1)
write_log(0, '192.168.0.1', 3)
作为替代方案,你可以考虑写你的记录功能作为一个Python类。这样可以节省你不必跟踪文件句柄,并允许您使用Python的with
声明:
class LogTimestamp:
def __init__(self, log_filename):
self.log_filehandle = open(log_filename, 'a')
def __enter__(self):
return self
def __exit__(self, *args):
self.log_filehandle.close()
def write_logline(self, text):
now = datetime.strftime(datetime.now(), '%H:%M')
log_text = '{} {}\n'.format(now, text)
print(log_text, end='') # also display log info, comment out if not needed
self.log_filehandle.write(log_text)
def write(self, response, hostname, option):
if response == 0:
self.write_logline('{} is up'.format(hostname))
if option == 1:
self.write_logline('{} is the option'.format(option))
self.write_logline('this is option number 1')
elif option == 2:
self.write_logline('{} is the option'.format(option))
self.write_logline('this is option number 2')
else:
self.write_logline('{} is the other option'.format(option))
self.write_logline('this is the result of other option')
# Write 3 entries to the log
with LogTimestamp('test.txt') as log:
log.write(0, '192.168.0.1', 1)
log.write(0, '192.168.0.1', 2)
log.write(0, '192.168.0.1', 3)
两个版本会给你一个输出文件看起来像:
09:51 192.168.0.1 is up
09:51 1 is the option
09:51 this is option number 1
09:51 192.168.0.1 is up
09:51 2 is the option
09:51 this is option number 2
09:51 192.168.0.1 is up
09:51 3 is the other option
09:51 this is the result of other option
如果”经常这样做,值得你学习和使用[Python日志记录](https://docs.python.org/3/howto/logging.html#logging-basic-tutorial)。 – Evert