code: def bwrite(stream, obj):
      
      handle = None
      if not hasattr(stream, "write"):
          stream = handle = open(stream, "wb")
      try:
          stream.write(bencode(obj))
      finally:
          if handle:
              handle.close()
docs: Encode a given object to a file or stream.