How to do python only read the file when it is complete

2

I'm doing a program that reads a .txt file that updates itself in the execution of another program in fortran and creates a real-time animation of the temperature map of a board, however it is giving an error because the fortran is updating the file in real time, because in some moments python opens the file before the fortran loop ends and picks up an incomplete array that will therefore crash the program. Any ideas on how to get the two of you together?

Follow the code in question below

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time

n=100
x = np.linspace(0, 99, num=100)
y = np.linspace(0, 99, num=100)
X,Y = np.meshgrid(x, y)

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)

def animate(i):
        Temp_matrix=np.empty(shape=[0,n])
        file = open('Temperature.txt', 'r')
        for i in range (1,n+1):
                line = file.readline().strip().split()
                line=np.float32(line)
                Temp_matrix=np.append(Temp_matrix,[line],axis=0)
        fig.clear()
        CS = plt.contourf(X,Y,Temp_matrix,9)
        colorb=plt.colorbar(CS)

        plt.title('Placa 1x1')

ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()
    
asked by anonymous 31.08.2018 / 15:29

0 answers