def bwrite(stream, obj):
handle = None<EOL>if not hasattr(stream, "<STR_LIT>"):<EOL><INDENT>stream = handle = open(stream, "<STR_LIT:wb>")<EOL><DEDENT>try:<EOL><INDENT>stream.write(bencode(obj))<EOL><DEDENT>finally:<EOL><INDENT>if handle:<EOL><INDENT>handle.close()<EOL><DEDENT><DEDENT>
Encode a given object to a file or stream.