I need to call a routine inside a loop which takes a text file as input. Because I don't want to open and close the text file all the time I keep it open in the loop. For Example:
with open("test.txt",'r+') as w_file:
    w_file.write(str(0.8) + "\n" + str(0.2))
    subprocess.call(["cat","test.txt"]) #Here I want to call my routine
But the file is still in the old state. Why? And whats the best way to handle this?

w_file.flush()after yourwriteline, but before thesubprocess.callline.print()function:print(0.8, 0.2, sep="\n",file=w_file)or string formatting:w_file.write("{}\n{}".format(0.8, 0.2))