Opening, Reading, and Writing to Text Files in Python
Being able to open, read, and write to text files in Python has several uses. Two examples of using text files may be to store temporary data in a game, or reading notes. With Python's built-in functions, we can work with text files with ease.
Opening a Text File
To work with a text file, we must first open it. The open function is used for this. It must take in a file path, which is the first parameter. There are optional parameters such as mode, which we'll use later on. The open funciton returns a file object that is used to be able to work with the file. Since we are not specifying a mode. the file will be opened in read mode.
Here's an example of how we can open a file:
text = file.read()
The read method can also take in an optional parameter of the number of bytes to return. Since nothing is specified above, the while file will be read.
Alternatively, there are other methods such as readline and readlines. If you want to read a single line, use readline. If you want to read each line stored as an item in a list, use readlines.
Writing to a Text File
We can write to a file using the write method, but we need to make sure the file is opened in write mode. If the file does not exist, the file will be created automatically. To specify that the file should be opened in write mode, we specify the mode as 'w'. Here are two ways we can do this:
You can pass in the mode as a second parameter:
file = open("your_filename.txt", "w")
Or you can specify the parameter name as follows. This helps provide more information as to what 'w' represents in the function call.
file = open("your_filename.txt", mode="w")
Now that we have a file object to work with, we can write to the file. Here, we'll write 'Hello' to the file:
file.write("Hello")
If there are contents in the file already, they will be deleted and then 'Hello' will be written to the file. But if we want to add to the contents and not delete anything, we can open the file in append mode, specified as 'a'.
file = open("your_filename.txt", mode="a")
Now, calling the write method will add our string to the end of the file.
Alternatively, there is a method writelines that can take a list of strings and write each string on a separate line.
Closing a Text File
When you are done using the file, make sure to close it so that resources can be freed up on your computer. If you leave an application that you aren't using running on your computer, it will take up space and could slow down your computer. The same concept applies to working with files. This is how we close the file:
file.close()
Use 'with' to Work with Text Files Without Forgetting to Close Them
We can also use the with keyword to open and work with text files in Python, and it's recommended to do so. This allows us to not have to worry about forgetting to close the file.
with open("your_filename.txt") as file:# Do something with the file
We still have access to our file object, which we called 'file', but you can still give it any variable name you want. Here's an example of reading the file:
with open("your_filename.txt") as file:text = file.read()
What We Learned Working with Text Files in Python
Here we learned how to open, read, and write to text files in Python. We also learned the value of closing files. There are other modes of working with files such as being able to both read and write, as well as working with binary formats. There are also other parameters that can be passed into the open function that were not discussed in this post.