How to Append Data to a File in Python?

I am working on a Python project where I need to append some data to a file. I have tried using the open() function with the a mode to append the data, but it’s not working as expected. Here’s the code I have tried:

Can anyone suggest a better way to append data to a file? I would also appreciate any suggestions on how to handle errors that may occur during the process.

Thanks in advance for your help!