File Tips

Learn how to use files safely in the Python program.

Let Python close files

A file is a resource. By one definition, a resource is something the program must request from the operating system, where the program can’t continue until the request is granted.

Further, a file is a reusable resource. Once obtained, it doesn’t disappear (like, say, a received network message) but must be returned to the operating system so that other programs could use it.

Use open()

A program obtains the right to use a file by opening it with the built-in function open(). When the program doesn’t need the file anymore, it should close it. Keeping the file open may present some grave consequences.

If we open a file for writing and write something into it, the data isn’t immediately written to the non-volatile storage like the disk, but is instead stored in the RAM buffers. If our program suddenly crashes, the data in the buffers is lost, and the file may be left in an inconsistent state. We should close our files for the sake of data integrity.

If we open too many files for reading or writing, we can’t open any more files. The limit is determined by the operating system and is usually quite large. We can check the limit by calling resource.getrlimit():

Get hands-on with 1400+ tech skills courses.