<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(notebook, file_or_stream, fmt, version=nbformat.NO_CONVERT, **kwargs):
 """Write a notebook to a file"""

    # Python 2 compatibility
    text = u'' + writes(notebook, fmt, version, **kwargs)
    file_or_stream.write(text)
    # Add final newline #165
    if not text.endswith(u'\n'):
        file_or_stream.write(u'\n')