code: def write_output(output, text=True, output_path=None):
      
  
      if output_path is None and text is False:
          print("ERROR: You must specify an output file using -o/--output for binary output formats")
          sys.exit(1)
  
      if output_path is not None:
          if text:
              outfile = open(output_path, "w", encoding="utf-8")
          else:
              outfile = open(output_path, "wb")
      else:
          outfile = sys.stdout
  
      try:
          if text and isinstance(output, bytes):
              output = output.decode('utf-8')
  
          outfile.write(output)
      finally:
          if outfile is not sys.stdout:
              outfile.close()
docs: Write binary or text output to a file or stdout.