Python 3 provides a built-in function that you can use to open files:
This way is the easy, naive one to read a text file:
my_file_handler = open("readme.txt", "r") my_file = my_file_handler.read() my_file_handler.close()
I’ll give you a better way to do so, but for now, let’s go over some definitions.
File Handler Parameters
open() the path to a file, and based on how you want to process the file, choose the mode to operate. There are a few basic options here:
- “r” — open a text file to read-only. Use this parameter for reading text files.
- “rb” — open a binary file to read-only. This parameter is best for reading binary files you saved to the disk or other files you need to process, and you know their binary structures (serialized objects, lists, classes, etc.)
- “w” — open a text file to write only. This parameter is best when you want to save lines of text to a file — like logs. When using this mode, you can select a non-existent path, and if Python can write to that location, it will create the file for you.
- “wb” — The same as “w” but for binary files. Save your lists, objects, dictionaries, and more to the file for later processing.
Managing a File
When you open a file as described above, you should never forget to close it as well. That means using the
close() function after you’re done with reading/writing the file.
Forgetting to do so may result in a corrupted file, memory leaks, and other unexpected issues in your code.
Python allows us to take a lot of the work of cleaning up the connection to a file behind the scenes — and also make our code more readable — by using the
with statement to create a specific context block.
The code below is how it looks like:
with open("my_json.json", "r") as json_file_handler: my_json = json_file_handler.read() # By this point the connection to the file is already closed. my_dict = json.loads(my_json)
You read the file in a specific block, and when the code block ends, the handler to the file automatically closes the file without you needing to call
close() and add another line of code.
Pretty awesome, no?
Not only do context managers make life easier when managing file handlers, but they also make your code cleaner.
In this article, I showed you the
open() method and how to use it both naively and using a context manager.
By using a context manager, we let python close the connection to a file for us. We can use context managers by using the
with statement wherever supported (and specifically
open() supports using one).
All code relating to the file management — like the
read() function — is indented under the
with statement. When that block ends, the file we opened will be closed automatically.
I hope that helped you discover another tool you can use to make your code more readable and elegant.
Thanks for reading, and let me know if you have any questions!