Python Forum

Full Version: File access confusion python windows
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This problem is difficult to explain, I hope I get the idea across ok. 

There is a json file on a Raspberry Pi. I have a continuously running program (Visual Basic) on my windows laptop that is reading and displaying the contents of that file via ssh.  It reads and updates the displayed data every 1 second.  Initially, on start, it has the correct data. There is a python sketch running on the Pi that occasionally updates the pi file. 

The problem occurs after python updates the file, the win program does not see that data changed UNTIL I first use win explorer to view the pi file. As soon as I open the file with explorer the continuously running win program instantly shows the correct data. If I use cat on the pi, it always shows the correct data in the file.

Here are the steps to make this happen:
1.    Start the win program loop and the data shown is correct.
2.    Update the json file with known data using a python sketch
3.    The data displayed in the win program shows the OLD data, not the data written in #2 above
 
Now the confusing part...  If I use the windows explorer to view that file, instantly, #3 above shows the correct data!!!

Here is the python code that updates the file
# write the status file data
     def write_status_file(self):
           self.status_succeded = False
           while True:
                time.sleep(.1)
                print("write")
                print(self.data)
                try:
                      with open('/home/pi/sprinkler/status.json', 'w') as self.stat:
                           json.dump(self.data, self.stat)
                           break
                except IOError as e:
                      print "I/O error({0}): {1}".format(e.errno, e.strerror)
                except ValueError:
                      print ("Can't write file status.json")
 
Here is the VB.net subroutine that is called once per second
Output:
    ' read and display the status.json file     Public Sub read_status_file()         Dim status_string As String = ""         Dim status_reader As System.IO.StreamReader         Dim retryCount As Integer = 0         Dim wasSuccessful As Boolean = False         Do             Try                 status_reader = New StreamReader("\\RASPI\pi\sprinkler\status.json")                 status_string = status_reader.ReadToEnd()                 status_reader.Close()                 wasSuccessful = True             Catch ex As Exception                 retryCount += 1             End Try         Loop Until wasSuccessful = True OrElse retryCount >= 100         'check if the statements were unsuccessful'         If Not wasSuccessful Then             Timer1.Enabled = False             MessageBoxTimer(1)             Timer1.Enabled = True         End If         status_file.Text = "status = " & status_string     End Sub
Here is the link to the image of both programs running. The top VB.Net program shows the status file as "stat":[0] when the lower Python program shows that it has written "stat":[3]

https://www.dropbox.com/s/2296hxqyj6hnum4/vb.jpg?dl=0