Python Forum
File access confusion python windows
Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
File access confusion python windows
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to access excel file on our sharepoint server but getting errors cubangt 0 773 Feb-16-2023, 08:11 PM
Last Post: cubangt
  data file for .exe program to access ose 2 1,145 Nov-23-2022, 08:02 PM
Last Post: snippsat
  batch file for running python scipt in Windows shell MaartenRo 2 1,825 Jan-21-2022, 02:36 PM
Last Post: MaartenRo
  Using a .bat file to execute simple anaconda commands in Windows 10 gacg1 0 4,625 Nov-30-2020, 03:24 PM
Last Post: gacg1
  Need to access Windows Machine or Personal Certificate Store and send to web app seswho 0 1,608 Sep-14-2020, 04:57 PM
Last Post: seswho
  Can't access the net using Python's Selenium module ShishirModi 2 2,009 Jul-21-2020, 06:03 AM
Last Post: ShishirModi
  Making .exe file that requires access to text and html files ClassicalSoul 0 1,552 Apr-23-2020, 05:03 PM
Last Post: ClassicalSoul
  python ast if-elif confusion holyghost 3 3,019 Apr-19-2020, 12:36 PM
Last Post: pyzyx3qwerty
  Windows fatal exception: access violation czhhua28 0 5,874 Mar-11-2020, 01:53 AM
Last Post: czhhua28
  Python for MS Access DB Kundan 0 1,847 Feb-20-2020, 08:27 AM
Last Post: Kundan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020