Can you explain how to close file? And Why should we close files?

Discussion RoomCategory: ProgrammingCan you explain how to close file? And Why should we close files?
Ashly asked 5 years ago

To close a file in Python we should use close() function. Mainly there is two reasons why we should close files after use. Firstly, Python does not promise that it will close the files for us. The operating system does, when the program exits. If your program does something else for a while, or repeats this sequence of steps dozens of times, we could run out of resources, or overwrite something. Second, some operating system platforms won’t let the same file be simultaneously open for read-only and for write. So, if the two filenames happened to be the same file, we might get an error trying to write without having closed the input file.entation.q Quit the debugger and exit.

Scroll to Top