Reputation: 1
Python's documentation says
When you’re done with a file, call f.close() to close it and free up any system resources taken up by the open file. After calling f.close(), attempts to use the file object will automatically fail.
How does Python file IO work and why does it mean to close a file? What resources are taken up by Python's file IO operations?
Upvotes: 0
Views: 94
Reputation: 191983
Maybe this analogy will help.
A file is a book.
You open a book -- In programming, this will create some reference variable in memory to point to the "front page" in read/write mode, or "last page" in append mode.
While the "book" is open, you can "read" it and "flip through the pages" -- In Python, this is done by open
then read
, readlines
, seek
, etc.
The "book" remains opened, then it takes up some space where it is opened. -- In programming, this is general Operating System memory. Think of this like space on your desk.
When you "close the book", then the space it occupies is freed. Think about this like taking the book off your desk and placing it back on a shelf, out of the way.
Closing again isn't possible because it is already closed.
Not closing the "book" and opening more "books" causes you to run out of space because you can only place so many books on the desk -- In programming, this would be comparable to a resource leak.
Upvotes: 0
Reputation: 310227
Generally speaking, your operating system has a limit to the number of file handles that can be opened for a particular process. This isn't specific to python but to any programming language operating on that system.
For example, on *nix systems, ulimit -n
will tell you the number of file handles you can have open.
Closing your file handles makes sure that you don't run into errors because the operating system refuses to let you open another file. :-).
Upvotes: 1