<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def writable(file_or_path, mode, encoding=None):
 """\ Returns a writable file-like object. Usage:: with writable(file_name_or_path, 'wb') as f: :param file_or_path: Either a file-like object or a filename. :param str mode: String indicating the writing mode (i.e. ``'wb'``) """

    f = file_or_path
    must_close = False
    try:
        file_or_path.write
        if encoding is not None:
            f = codecs.getwriter(encoding)(file_or_path)
    except AttributeError:
        f = open(file_or_path, mode, encoding=encoding)
        must_close = True
    try:
        yield f
    finally:
        if must_close:
            f.close()