While doing file operations, we might need to append text to an existing file without erasing the existing data. In this article, we will discuss how we can append text to a file in python.
Append text to file using write() method
To append a text to a file using the write()
method, we first need to open the file in append mode. For this, we will use the open()
function with the file name as its first parameter and “r+
” as the second parameter. After opening the file, we can simply append the text to the file using the write()
method. The write()
method is invoked on a file object and takes the text that need to be appended to the file as its input parameter. You can observe this entire process below.
myFile = open("sample.txt", mode="r+")
print("The content of the file before modification is:")
text = myFile.read()
print(text)
myString = "This string will be appended to the file."
myFile.write(myString)
myFile.close()
myFile = open("sample.txt", "r")
print("The content of the file after modification is:")
text = myFile.read()
print(text)
Output:
The content of the file before modification is:
This is a sample file.
The content of the file after modification is:
This is a sample file.This string will be appended to the file.
After appending the text to the file, don’t forget to close the file. Otherwise, the content will not be saved. Here, we have used the read() function to verify the contents of the file before and after appending the text.
Append text to file using the print() function
Normally, when we use the print()
function, it prints the values to the standard input. However, we can also use the print()
function to append text to a file in python. The print()
function has an optional parameter “file
”. Using this parameter, we can specify where to print the values that are passed as input to the print()
function.
To append the text to the file, we will first open the file in append mode using the open()
function. After that, we will pass the text and the file object to the print function as the first and the second input arguments respectively. After execution of the print()
function, the text will be appended to the file.
myFile = open("sample.txt", mode="r+")
print("The content of the file before modification is:")
text = myFile.read()
print(text)
myString = "This string will be appended to the file."
print(myString, file=myFile)
myFile.close()
myFile = open("sample.txt", "r")
print("The content of the file after modification is:")
text = myFile.read()
print(text)
Output:
The content of the file before modification is:
This is a sample file.
The content of the file after modification is:
This is a sample file.
This string will be appended to the file.
In the output, you can observe that myString
has been appended in the file in a new line. When we did the same operation using the write()
method, myString
was appended to the last line of the existing file. So, you can use this difference to choose the proper approach according to your requirement. Also, Make sure that you close the file after appending the text to it. Otherwise, the changes will not be saved.
Conclusion
In this article, we have discussed two ways to append text to a file in python. To learn more about file operations, you can read this article on file handling in python. You might also like this article on list comprehension in python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.