简体   繁体   中英

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

In some codes like MCMC it lasts for hours and maybe days to finish. Now I am wondering how can see the outputs which are saving in a text file while Python is running. Because in my code checking the whole outputs in txt file is possible only after finishing the Python's work

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

with this code, I can only see the outputs after finishing the python running. But it is beneficial if we can check it every time we want.

#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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM