Hurry! Try our new Interactive Courses for FREE. 🥳   🚀
  Signup/Sign In

How to append text to a file in python

As the part of programming requirement, we have to store our data permanently for future purposes. For this requirement, we should go to files. Files are very common permanent storage areas to store our data.

In this tutorial, we will learn how to append text to a file using the write() and writelines() functions and append() functions.

Before performing any operation (like read or write) on the file, first, we have to open that file. For this, we should use Python's in-built function open(). But at the time of open, we have to specify the mode, which represents the purpose of opening the file. After completing our operations on the file, it is highly recommended to close the file. For this, we have to use the close() function.

The allowed modes in Python are:

Sl.no Modes Explanation
1. r open an existing file for read operation.
2. w open an existing file for a write operation.
3. a open an existing file for append operation.
4. r+ To read and write data into the file.
5. w+ To write and read data.
6. a+ To append and read data from the file.
7. x To open a file in exclusive creation mode for a write operation.

Example: Writing text to a File

We can write character data to the text files by using write(str) and writelines(list of lines). Consider an example to write the data to a file using both write() and writelines () functions.

f=open("file1.txt",'w')
f.write("study\n")
f.write("tonight\n")
f.write(".com\n")
f.writelines("python is very easy")
print("Data written to the filename file1.txt successfully")
f.close()

While writing data by using write() methods, compulsory we have to provide a line separator(\n), otherwise total data should be written to a single line.

Once we run the code, it shows the following result.


Data are written to the filename file1.txt successfully
file1.txt
study
tonight
.com
python is very easy

In the above program, the data present in the file will be overridden every time if we run the program.

Instead of overriding if we use append operation then we should open the file as follows.

Example: Appending Text to a file

To append data, use the append mode in the open() function that allows writing the data to the existing file. We cannot read that file. We can check if it is readable or writable using the functions readable() and writable().

f=open("file2.txt",'a')
data=["New Delhi, national capital of India\n","It is situated in the north-central part of the country\n"," on the west bank of the Yamuna River"]
f.writelines(data)
print("Is File Readable:  ",f.readable())
print("Is File Writable:  ",f.writable())
print("Lines append to the filename file2.txt successfully")
f.close()

Once we run the code, it shows the following result, and in the file2.txt file data written like this.


Is File Readable: False
Is File Writable: True
Lines append to the filename file2.txt successfully

In the file2.txt file.
New Delhi, the national capital of India
It is situated in the north-central part of the country
on the west bank of the Yamuna River

Example: Appending Text to a File

To append data, use the append mode(a+) in the open() function that allows writing the data to the existing file. It allows to write and read the data from the existing file.

f=open("file2.txt",'a+')
data=["New Delhi, national capital of India\n","It is situated in the north-central part of the country\n"," on the west bank of the Yamuna River"]
f.writelines(data)
print("Is File Readable:  ",f.readable())
print("Is File Writable:  ",f.writable())
print("Lines append to the filename file2.txt successfully")
f.close()

Once we run the code, it shows the following result.


Is File Readable: True
Is File Writable: True
Lines append to the filename file2.txt successfully

In the file2.txt file.
New Delhi, the national capital of India
It is situated in the north-central part of the country
on the west bank of the Yamuna River

Conclusion

In this tutorial, we learned about files and their modes, how to write text to the files by solving examples.