Trying to sort a list with fruit.sort() : I am trying to sort the list (fruit) after I have appended the contents of another file to it, and prior to writing it to the the txt file "fruit_salad.txt".

The problem I am having is that as soon as I write. "fruit.sort()" it then fails to recognise output.

What am I doing by sorting a list that causes this?

Thank you in advance.


----------
---CODE---

    try:
        f = open("fruit.txt", mode='r', encoding='utf-8')
        fruit = f.readlines()
        print(fruit, "\n")


        f1 = open("more_fruit.txt", mode="r", encoding='utf-8')
        fruit.append(f1.readlines())
        print(fruit)
        # fruit.sort() # WITHOUT THIS LINE IT WORKS FINE
        output = open("fruit_salad.txt", mode='w', encoding='utf-8')
    
        for line in fruit:
            output.writelines(line)

    except (FileNotFoundError, IOError):
        print("File Not Found!!")
    finally:
        f.close()
        f1.close()
        output.close()
 
----------
--OUTPUT--

['pear\n', 'apple\n', 'orange \n', 'mandarin\n', 'watermelon\n', 'pomegranate\n', 'lemon\n', 'pineapple\n'] 

['pear\n', 'apple\n', 'orange \n', 'mandarin\n', 'watermelon\n', 'pomegranate\n', 'lemon\n', 'pineapple\n', ['banana\n', 'raspberry\n', 'blueberry\n', 'lime\n', 'blackberry\n', 'cherry\n', 'grape']]
The program 'python.exe' has exited with code 0 (0x0).


----------
ERR OUTPUT

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Extensions\Microsoft\Python\Core\ptvsd_launcher.py", line 111, in <module>
    vspd.debug(filename, port_num, debug_id, debug_options, run_as)
  File "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Extensions\Microsoft\Python\Core\Packages\ptvsd\debugger.py", line 36, in debug
    run(address, filename, *args, **kwargs)
  File "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Extensions\Microsoft\Python\Core\Packages\ptvsd\_main.py", line 47, in run_file
    run(argv, addr, **kwargs)
  File "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Extensions\Microsoft\Python\Core\Packages\ptvsd\_main.py", line 98, in _run
    _pydevd.main()
  File "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Extensions\Microsoft\Python\Core\Packages\ptvsd\pydevd\pydevd.py", line 1628, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Extensions\Microsoft\Python\Core\Packages\ptvsd\pydevd\pydevd.py", line 1035, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Extensions\Microsoft\Python\Core\Packages\ptvsd\pydevd\_pydev_imps\_pydev_execfile.py", line 25, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:\Users\micha\source\repos\PythonFileManagement\PythonFileManagement\PythonFileManagement.py", line 24, in <module>
    output.close()
NameError: name 'output' is not defined