2 Methods to write text file in Python

In this article, we will learn about 2 Methods to write text file in Python or append their contents. To perform a write operation on a file the file must be in the open state. If the file is not opened then it is not possible to do the read and write operation.

Similarly, after we are done with the write operation, then it is highly recommended to close the file for proper file data flushing.

Two types of files handle in Python

  • Text File : The data is store in text format and Each text line end withth special charaacter EOF character that is newline.This is defulat file type in python.
  • Binary File : The data is store in binary format(0,1) there is no termination of line.

File Modes for writing a file

These are the list of modes in Python that we can use to open a file in writing mode as per need.

Mode Details
wopen file for writing plain text
w+open file for writing and reading plain text
aOpen File in append mode and content is appended to the end of file
a+Open File in append mode if already exist and append content are appended to the end of file else create a new file

1. Steps to write text file in Python

  • Open a Text File.
  • Write a text File.
  • Append a Text File.
  • Close a text File.

1.1 Open a text File in Python

The File built-in Open() function is used to open files in writing mode. It takes multiple arguments. The main is the first parameter FilePath and the second is an optional file mode.


file_open = open("file_path","mode")
  • File_path : It specify the path/location where we are creating a new file.
  • Mode: It specifies the type of operation performing on file.

Using file object

fileobj = open("myfile","w")

using with keyword

Another short syntax to open and close the file by using with keyword

with open('file_path') as file_open:

1. 2 CLose a text file in Python

The close() method is used to close a file if open using the file object. Once we have done writing the data into a file It is good practice to close the file to free the memory space.


The close() method is not a safe method to close a file . The best way to close a file using with statement.

with open('file_path') as file_open:

2.1 Methods to write a text file in Python?

Moving to the next step is writing the file with some data content. we have file built-in write() and writelines() function which is again implemented in the file object which we get when we open a file. So we will write a line of text to our file by using the write() function. or multiple using Writelines()

2.1. Write() Method for writing a line in Text

This function is used to write a single line in a file. This method truncates a file means all the existing data will be erased if already exists else create a new file.

We must add a newline character(\n) to the end of each line.

This will create a text file in the current working directory and append a new character at end of each line and finally close the file using close() method

file_open = open("samplefile.txt","w+")
file_open.write("This is my File content\n")
file_open.write("writing text to file \n")

2.2 write() method with statement

Using with statement to open a file no need to use the file.close() method.It itself handle the release of resource and free memory space.

with open("samplefile.txt","w+") as file_open:
 file_open.write("This is my File content\n")
 file_open.write("writing text to file \n")

2. 3Writelines() to Write a text file in Python

This is used to write multiple lines in a file in python, we can write a list of lines using this function.

In this example, we are writing a list of strings in a text file.

with open('samplefile.txt','w+') as file_open:

 List_of_lines = ["This is text_file \n","This is first text file \n","A text file \n"] 


3. How to Append Data to a File

The append mode opens the file in append mode and the content is appended at the end of the file.

By using “a+”, You can append/add a new text to the already existing file or a new file. The plus(+) with ‘a ‘ indicates that it will create a new file if does not exist already else append it at the end of an existing file.

Append Data to a File

file_open = open("\nsamplefile.txt", "a+")
file_open.write("welcome to devenum ")