new_contents: import os
  import datetime
  
  from contextlib import contextmanager
  
  @contextmanager
  def atomic_write(filepath, binary=False):
      tmppath = filepath + '~'
      while os.path.isfile(tmppath):
          tmppath += '~'
      try:
          with open(tmppath, 'wb' if binary else 'w') as file:
              yield file
              file.flush()
              os.fsync(file.fileno())
          os.rename(tmppath, filepath)
      finally:
          try:
              os.remove(tmppath)
          except (IOError, OSError):
              pass
  
subject: add atomic open/write operation