更新 txt 文件以检查 python 将数据保存到其中

Updating the txt file to check while python is saving the data into it

在某些代码中,例如 MCMC,它会持续数小时甚至数天才能完成。 现在我想知道如何查看 text file 中保存的输出,而 Python 是 运行。因为在我的代码中,只有在完成 Python 的工作后才能检查 txt file 中的整个输出

def ....():
   return
def ....():
   return
......
with open('outputs/p.txt', 'w') as f:
 .....
   f.write("{0}\t{1}\n".format(A,B))

使用此代码,我只能在完成 python 运行 后才能看到输出。但是,如果我们可以随时检查它,那将是有益的。

#the a+ appends the file at the end with your new data, or creates the file if it doesn't exist
with open('outputs/p.txt', 'a+') as f:

    f.write("{0}\t{1}\n".format(A,B))

    f.close()