<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def write_encoded(file_obj, stuff, encoding='utf-8'):
 """ If a file is open in binary mode and a string is passed, encode and write If a file is open in text mode and bytes are passed, decode and write Parameters file_obj: file object, with 'write' and 'mode' stuff: str or bytes, stuff to be written encoding: str, encoding of text """

    binary_file = 'b' in file_obj.mode
    string_stuff = isinstance(stuff, basestring)
    binary_stuff = isinstance(stuff, bytes)

    if not PY3:
        file_obj.write(stuff)
    elif binary_file and string_stuff:
        file_obj.write(stuff.encode(encoding))
    elif not binary_file and binary_stuff:
        file_obj.write(stuff.decode(encoding))
    else:
        file_obj.write(stuff)
    file_obj.flush()
    return stuff