title: Python mmap.write() writes not only in the memory, but also on the disk
body: <p>I'm on the Windows, using mmap module for sharing information between two Python scripts. My problem is to write data into a memory for being accessed from other script, without writing it on a disk. According to <a href="https://docs.python.org/3.6/library/mmap.html?highlight=mmap" rel="nofollow noreferrer">documentation</a>, <code>mmap.write()</code> method is exactly what I'm looking for: <strong>Write the bytes in bytes into memory at the current position of the file pointer and return the number of bytes written</strong>. </p>
  
  <p>I'm running the following code to test writing into memory and face 2 problems:</p>
  
  <pre><code>def test_write_mmf(json_path):
  
      # Know the file size to read from the memory
      file_size = os.path.getsize(json_path)
  
      # Open and read file amd MMF
      json_file = open(json_path, "r+", encoding="utf8")
      mapped_file = mmap.mmap(json_file.fileno(), 0)
      mapped_file.seek(0)
      buffer_for_json = mapped_file[:file_size]
  
      # Content is JSON, so load it
      json_data = json.loads(buffer_for_json.decode("utf-8"))
  
      # do some in-memory edits
      json_data[0]["response_index"] = 0
      json_data[0]["description"] = "INSTALL"
  
      # write it back into memory
      json_string = json.dumps(json_data)
      json_bytes = bytearray(json_string, encoding="utf8")
      mapped_file.seek(0)
  
      # It supposed to write into memory only!
      mapped_file.write(bytes(json_bytes))
  
      # Here I wait to check if the file was written
      m.getch()
  
      # Supposed to be real flush content to disk
      mapped_file.flush()
  </code></pre>
  
  <ol>
  <li><p>At the line <code>mapped_file.write(bytes(json_bytes))</code> I'm expecting my changes are still in the memory, but it is in a file on the disk already. </p></li>
  <li><p>This problem is not connected to previous, but I realized when the written content smaller than it was before, I see the "tail" of the previous data still remains in the rewritten file. Is there a way to erase opened file? I tried <code>json_file.truncate()</code> with Permission Denied <code>(WinError=13)</code> error.</p></li>
  </ol>