Why is closing the file in python(or other languages) necessary? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why is closing the file in python(or other languages) necessary?

Please explain practically

7th Sep 2019, 8:24 AM
Sarthak
Sarthak - avatar
7 Answers
+ 3
✳AsterisK✳ I know that. I mean to say, In programming, how actually it affects the code environment or whatever it influences. Leaving books or copies open doesn't make any sense directly on the subject of its protection. I had heard that, it is related to data leakage from file and similar views on not closing files in programming language. I want to know, what actually it affects in coding environment. I hope I'll get answer soon.
7th Sep 2019, 10:24 AM
Sarthak
Sarthak - avatar
+ 3
✳AsterisK✳ But as far as I've heard, when the program stops running, the whole memory it uses gets cleared. When the program stops, the file data opened in ram is cleared.
7th Sep 2019, 10:30 AM
Sarthak
Sarthak - avatar
+ 3
Just as an example, in Java, when you use BufferedWriter you need to close the file to make sure everything that you wrote in the file is actually written. In order to enhance the performance bufferedwriter is not writing to the file immediately, by closing the file you let java know that you want everything in buffer mode written in the file.
9th Sep 2019, 4:38 AM
Amir Gh
Amir Gh - avatar
+ 2
when you're through reading or writing in school do you leave your books open and on the desk or you close em and pack it up??? the one you chose answer your question
7th Sep 2019, 10:17 AM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 2
when you leave a file open it takes space on your memory as if its still in use, the only chance you get is if the operating system helps you to clear the memory space taken by the open file when you restart your OS, just like on the table you open alots of books without packing em soon the table won't be able to take any more books and you won't feel convenient writing on the table
7th Sep 2019, 10:28 AM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 2
that is not guaranteed, i don't know if such happens in dynamic languages but in most static languages you have to do it all yourself
7th Sep 2019, 10:32 AM
✳AsterisK✳
✳AsterisK✳ - avatar