我正在创建一个逐行记录的日志文件。在
1-如果文件不存在,它应该创建文件并追加标题行和记录
2-如果存在,检查第一行中的文本timeStamp
。如果存在,则追加记录,否则添加标题列和记录本身
我试过w,a和r+,但都不管用。以下是我的代码:
logFile = open('Dump.log', 'r+')
datalogFile = log.readline()
if 'Timestamp' in datalogFile:
logFile.write('%s\t%s\t%s\t%s\t\n'%(timestamp,logread,logwrite,log_skipped_noweight))
logFile.flush()
else:
logFile.write('Timestamp\t#Read\t#Write\t#e\n')
logFile.flush()
logFile.write('%s\t%s\t%s\t%s\t\n'%(timestamp,logread,logwrite,log_skipped))
logFile.flush()
如果文件不存在,代码将失败
使用
'a+'
模式:说明:
以下代码将起作用:
试试这个:
相关问题 更多 >
编程相关推荐