Search code examples
pythontextoutputsaving-data

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.


Solution

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