code: def open_atomic(filename, binary=True):
      """Open a file for atomic writing. Instead of locking this method allows
      you to write the entire file and move it to the actual location. Note that
      this makes the assumption that a rename is atomic on your platform which
      is generally the case but not a guarantee.
  
      http://docs.python.org/library/os.html#os.rename
  
      >>> filename = 'test_file.txt'
      >>> if os.path.exists(filename):
      ...     os.remove(filename)
  
      >>> with open_atomic(filename) as fh:
      ...     written = fh.write(b'test')
      >>> assert os.path.exists(filename)
      >>> os.remove(filename)
  
      """
      assert not os.path.exists(filename), "%r exists" % filename
      path, name = os.path.split(filename)
  
      # Create the parent directory if it doesn't exist
      if path and not os.path.isdir(path):  # pragma: no cover
          os.makedirs(path)
  
      temp_fh = tempfile.NamedTemporaryFile(
          mode=binary and "wb" or "w",
          dir=path,
          delete=False,
      )
      yield temp_fh
      temp_fh.flush()
      os.fsync(temp_fh.fileno())
      temp_fh.close()
      try:
          os.rename(temp_fh.name, filename)
      finally:
          try:
              os.remove(temp_fh.name)
          except Exception:
              pass
  
imports: import os
  import time
  import atexit
  import tempfile
  import contextlib
  from .None import exceptions
  from .None import constants
  from .None import portalocker
label_desc: Use image node_id 1 for calling a global function with example usage: open_atomic(filename, binary) without return types