我只是在处理文本文件和如何在每次调用函数时写入一个新行来创建一个列表有点麻烦。在
if speedCarMph > 60:
f = open('Camera Output.txt', 'r+')
f.write("{} was travelling at {}MPH at {} and has broken the law".format(licensePlate, speedCarMph, camInput2) + "\n")
f.write("-----------------------------------------------------------------------------------------------------------")
f.close()
DeltaTimeGen()
else:
DeltaTimeGen()
我想在每次传递这个函数和调用函数时都写入文本文件的新行。在
使用
a
进行追加,如果有循环,还应在循环之外打开文件:r+
打开进行读写操作,因此当您打开文件时,指针将位于文件的开头,因此将写入第一行而不是附加到该行。在如果函数反复调用自己,那么最好使用
^{pr2}$while
循环。在也许在检查之间加一个
time.sleep
。在您只能打开一次文件,并在程序退出时将其关闭。只需在
f.write
行的末尾添加一个“\n”。如果需要刷新文件(以便立即显示输出),可以指定零缓冲:相关问题 更多 >
编程相关推荐