title: Google Colab - frequently write to a drive file
body: <p>I'm trying to frequently write some data from a Colab notebook to a file located on Google Drive. I mounted the drive with the<code>drive.mount('/content/drive')</code> function. Afterwards I have a loop that is writing to a file every <code>n</code> iterations, what is implemented like:</p>
  
  <pre><code>for i in range(BIG):
    # Do something...
    if i % n == 0:
      with open(gdrive_file_path, "w") as f:
        f.write("Some message")
  </code></pre>
  
  <p>I noticed that if the <code>n</code> is not big enough (like <code>n=10</code>) the content is not updated and the file is overwritten after the loop's end. If the <code>n</code> is bigger (tried <code>n=100</code>) I can see the changes live. I assume that the <code>colab.drive</code> requires some operational time to have things done, but I can't find a way to ensure that the drive file is updated without stopping the loop. I hoped that <code>f.flush()</code> and <code>os.fsync(f)</code> could help, but there was no improvement, calling the <code>flush</code> method leads to writting an empty content after the loop terminatiom. Is there any way to force the full and successful frequent file upload? </p>
  
  <p>Thanks for help!</p>