Why is it necessary to close the file?
it's is not necessary if you use your file once, your program works fine if you do not close it. But if you use more than once its needed to close your file. Although its always good and safer close it after use it.
it saves the text file
I think there is one more advantage in using "with". Suppose you are working with a series of large files, where you are reading the content of each file to memory and do some computation. If you forget to close each file properly, you may quickly run out of memory or you may cause the OS to do unnecessary swaps, which affects the system performance.
to free up memory used during file connection. just like when connect or disconnecting a rational db.