code: def write(
      nb,
      fp,
      version=nbformat.NO_CONVERT,
      fmt=None,
      config=None,
      **kwargs,
  ):
      """Write a notebook to a file name or a file object
  
      :param nb: the notebook
      :param fp: a file name or a file object
      :param version: see nbformat.write
      :param fmt: (optional if fp is a file name) the jupytext format like `md`, `py:percent`, ...
      :param config: (optional) a Jupytext configuration object
      :param kwargs: (not used) additional parameters for nbformat.write
      """
      if fp == "-":
          # Use sys.stdout.buffer when possible, and explicit utf-8 encoding, cf. #331
          content = writes(
              nb, version=version, fmt=fmt, config=config, **kwargs
          )
          try:
              # Python 3
              sys.stdout.buffer.write(content.encode("utf-8"))
          except AttributeError:
              sys.stdout.write(content.encode("utf-8"))
          return
  
      if not hasattr(fp, "write"):
          # Treat fp as a file name
          fp = str(fp)
          _, ext = os.path.splitext(fp)
          fmt = copy(fmt or {})
          fmt = long_form_one_format(fmt, update={"extension": ext})
          create_prefix_dir(fp, fmt)
  
          with open(fp, "w", encoding="utf-8") as stream:
              write(
                  nb,
                  stream,
                  version=version,
                  fmt=fmt,
                  config=config,
                  **kwargs,
              )
              return
      else:
          assert (
              fmt is not None
          ), "'fmt' argument in jupytext.write is mandatory unless fp is a file name"
  
      content = writes(
          nb, version=version, fmt=fmt, config=config, **kwargs
      )
      if isinstance(content, bytes):
          content = content.decode("utf8")
      fp.write(content)
      if not content.endswith("\n"):
          fp.write("\n")
  
imports: import logging
  import os
  import sys
  import warnings
  from copy import copy, deepcopy
  import nbformat
  from nbformat.v4.nbbase import NotebookNode, new_code_cell, new_notebook
  from nbformat.v4.rwbase import NotebookReader, NotebookWriter
  from .cell_metadata import _IGNORE_CELL_METADATA
  from .formats import _VALID_FORMAT_OPTIONS, divine_format, format_name_for_ext, get_format_implementation, guess_format, long_form_one_format, read_format_from_metadata, rearrange_jupytext_metadata, update_jupytext_formats_metadata
  from .header import encoding_and_executable, header_to_metadata_and_cell, insert_jupytext_info_and_filter_metadata, insert_or_test_version_number, metadata_and_cell_to_header
  from .languages import default_language_from_metadata_and_ext, set_main_and_cell_language
  from .metadata_filter import filter_metadata, update_metadata_filters
  from .myst import MYST_FORMAT_NAME, myst_extensions, myst_to_notebook, notebook_to_myst
  from .pandoc import md_to_notebook, notebook_to_md
  from .pep8 import pep8_lines_between_cells
  from .quarto import notebook_to_qmd, qmd_to_notebook
  from .version import __version__
label_desc: Use image node_id 5 for calling a global function with example usage: write(nb, fp, version, fmt, config) without return types