code: def write(
      nb,
      fp,
      version=NO_CONVERT,
      capture_validation_error=None,
      **kwargs,
  ):
      """Write a notebook to a file in a given nbformat version.
  
      The file-like object must accept unicode input.
  
      Parameters
      ----------
      nb : NotebookNode
          The notebook to write.
      fp : file or str
          Any file-like object with a write method that accepts unicode, or
          a path to write a file.
      version : int, optional
          The nbformat version to write.
          If nb is not this version, it will be converted.
          If unspecified, or specified as nbformat.NO_CONVERT,
          the notebook's own version will be used and no conversion performed.
      capture_validation_error : dict, optional
          If provided, a key of "ValidationError" with a
          value of the ValidationError instance will be added
          to the dictionary.
      """
      s = writes(nb, version, capture_validation_error, **kwargs)
      if isinstance(s, bytes):
          s = s.decode("utf8")
  
      try:
          fp.write(s)
          if not s.endswith("\n"):
              fp.write("\n")
      except AttributeError:
          with Path(fp).open("w", encoding="utf8") as f:
              f.write(s)
              if not s.endswith("\n"):
                  f.write("\n")
  
file_path: nbformat/nbformat/__init__.py
imports: from __future__ import annotations
  from pathlib import Path
  from traitlets.log import get_logger
  from .None import v1, v2, v3, v4
  from ._version import __version__, version_info
  from .sentinel import Sentinel
  from .None import reader
  from .converter import convert
  from .notebooknode import NotebookNode, from_dict
  from .v4 import nbformat
  from .v4 import nbformat_minor
  from .validator import ValidationError, validate
label_desc: Use image node_id 4 for calling a global function with example usage: write(nb, fp, version, capture_validation_error) without return types