Search code examples
pythonpython-3.xoperating-systemfile-handlingfile-copying

Read a file while some other program is writing the file in python


I am reading a file continously and another program modifies it. When i try to read the it only prints blank spaces.

File that reads

   import os
   import time
   f=open("file.py","r",os.O_NONBLOCK)
   while 1:
     x=f.read()
     if x.find("bye")!=-1:
        break
     else:
        time.sleep(1)
   f.close()

File that writes

  import os
  f=open("file.py","w",os.O_NONBLOCK)
  f.write("bye")
  f.flush()
  f.close()

file.py

  hello

The program only prints blank spaces


Solution

  • What you are trying to do should be fairly easy. I am pretty sure your code will technically work, but you really should handle files using a context manager. I also restructured your code to do what I think you intended to do a little better.

    File that reads

        import os
        import time
       
        we_have_written_bye = False 
    
        while we_have_written_bye = False:
            with open("file.py", "r") as f         
                x = f.read()
                if x.find("bye")!=-1:
                    we_have_written_bye = True
    
         # file closes automatically with a context manager so 
         # this is removed. Note, if bye was not written yet, we
         # close the file, then wait for a second by sleeping below
         
         time.sleep(1)
        
       
    

    File that writes

      import os
      
      with open("file.py", "w", os.O_NONBLOCK) as f 
        f.write("bye")
        f.flush() # not sure why you want to flush the file contents here
        f.close()
    

    file.py

      hello
    

    The two programs should work seamlessly. This is because the file object can not be opened if another program is writing to it. You may run into an issue with this, but if the writes are small I believe the standard library will wait long enough for the file lock to be given up.

    For a tutorial on context managers please see:

    https://www.youtube.com/watch?v=Lv1treHIckI

    this is part of a great series of semi-advanced python tutorials that will certainly up your game. Helped me tonnes