是的,有很多类似的问题,但是对于像这样的特定场景,我没有发现任何问题。 (如果有人真的偶然发现了另一个回答该问题的问题,我将删除该问题。请让我知道。)
click_time = time.strftime('%I:%M:%S %p')
with open('test.txt', 'a') as f:
#f.write('click: ' + str(x) + ' on %s\n' % click_time)
#f.write('[ %s ]#' + str(x).zfill(4) + ' to scroll.\n' % click_time)
顶部的f.write
即使抛出相同的错误,实际上也会按预期输出,以创建类似这样的文件...
[ 04:29:07 PM ] Created file. # created elsewhere in the code
click: 1 on 04:29:07 PM
click: 2 on 04:29:07 PM
click: 3 on 04:29:08 PM
click: 4 on 04:29:08 PM
click: 5 on 04:29:08 PM
click: 6 on 04:29:09 PM
click: 7 on 04:29:09 PM
click: 8 on 04:29:09 PM
click: 9 on 04:29:10 PM
click: 10 on 04:29:10 PM
click: 11 on 04:29:10 PM
click: 12 on 04:29:11 PM
click: 13 on 04:29:11 PM
click: 14 on 04:29:11 PM
[ 04:29:11 PM ] Finished scrolling. # created elsewhere in the code
底部的f.write
仍会输出,但输出基本上已中断。
[ 04:32:05 PM ] Created file. # created elsewhere in the code
[ 04:32:05 PM ] Finished scrolling. # created elsewhere in the code
我想知道为什么会出现差异? 为何尽管出现错误,顶部的f.write
仍能工作,而底部的f.write
却无法产生某些输出?