In the previous blog, we took a brief look at the concept of File Handling in Python as well as how to open a file in Python. In this blog, we will cover how we can read from a file, append to a file and write to a file. Remember, we will be using the same text we used in the previous blog.
Reading
We brushed through this in the last blog but let us take a closer look at it. There are 2 ways we can read through the contents of a file:
file_ = open("file_handling_contents.txt", "r") # The results are same for both for contents in file_: # Method 1 print(contents, end="") print(file_.read()) # Method 2
We can also use the read() function to print the specified number of characters from a text file.
print(file_.read(19))
This gives us:
Another function we can use is the readline() function, which prints the first line of a text.
print(file_.readline())
This gives us:
Our first line has a “Hello!” as well as a newline in the text, and this will be reflected in our output.
We can use readline() multiple times to print out each line one by one.
print(file_.readline()) print(file_.readline()) # Prints the second newline print(file_.readline())
Appending & Writing
Let’s look at how we can append content to a text file. To either append or write content to a text file, we need to use the write() function, however, the distinction lies in the mode we select.
For appending we need to use “a” as the mode:
# Code for appending file_append = open("file_handling_contents.txt", "a") file_append.write("Added Text") file_append.close() # Code for reading file_ = open("file_handling_contents.txt", "r") print(file_.read()) file_.close()
This gives us the output:
NOTE: This modifies our original file, so keep in mind that the next time you access a file to which you added something, your changes will be present. For the sake of the blog, I have restored the original text after appending it to it.
For writing we need to use “w” as the mode:
# Code for writing file_append = open("file_handling_contents.txt", "w") file_append.write("Previous content has been erased with only this line.") file_append.close() # Code for reading file_ = open("file_handling_contents.txt", "r") print(file_.read()) file_.close()
The output is:
We can also use a variable to store content in a new file, as shown below:
file_append = open("file_handling_contents.txt", "w") new_text = '''Verification Master Technical Writing to explain Python''' file_append.write(new_text) file_append.close()
If we try to write or append to a file that does not exist Python will create a file with that name and add content to it. The following cases show when Python will create a new file, if it does not exist:
file_modify = open("new_file.txt", "a") # mode - append file_modify = open("new_file.txt", "w") # mode - write
If we use the following and there exists a file with same name, we will get an error:
file_modify = open("new_file.txt", "x") # mode - create
NOTE: Remember to also mention the extension (such as .txt for a text file) of our file when we are creating a new file.
What have we learned?
- What is another way to read the contents of a file?
- How do we view a select number of characters of a file?
- How does the read() function differ from the readline() function?
- Which function do we use to append or write to a file in Python?
- How do we distinguish between appending or writing to a file?
- Which mode creates a new file?