Skip to content Skip to sidebar Skip to footer

File Buffer Flushing And Closing In Python With Variable Re-assign

Had an interesting experience with Python's file buffering and wanted to know that I understand it correctly. Given [Python 2.7 shell] ... model = (really big Numpy model) f = open

Solution 1:

As long as your computer does not crash, you won't lose data by not closing a file. Python does indeed close files if the corresponding file objects are garbage collected. In the case you described, the name f was the only reference to the file, so it was closed when you used the name for something else.

Note that it is good practice to close files anyway to free the system ressources allocated by the file object. In some situations you don't know exactly when a file object will be garbage collected -- for example in case of an error, a reference to the file object might be stored in the traceback object, preventing garbage collection. All files are closed when the interpreter exits.


Solution 2:

pickle dumps by default using is an ASCII format (protocol 0), so the length depends on the actual data. To use a binary dump you can pass -1 as version (note however that this will require the use of "wb" and "rb").


Post a Comment for "File Buffer Flushing And Closing In Python With Variable Re-assign"