Latest Code Tutorials

How to Write Line to File in Python

Python has various methods for creating, reading, writing, updating, and deleting files. In addition, Python allows users to handle files, i.e., to read and write files, and many other file handling options to operate on files. Let’s see how to write a file line by line in Python with different scenarios.

Python write to file.

To write a file in Python, use the open() function and add a parameter either “a” or “w”. To append content at the end of the file, use the “a” parameter. To overwrite any existing content in the file, use the “w” parameter. The open() is a built-in Python file handling function that opens and returns the file as a file object.

f = open("app.txt", "a")
f.write("File is updated with appended content!")

#open and read the file after the appending:
f = open("app.txt", "r")


11 12File is updated with appended content!

In this example, you can see that the output starts with 11 12. So that means 11 and 12 are already there in the file, and it just appended, “File is updated with appended content!”.

Python writes a line to file.

To write a line to a file in Python, use a with open() function. The with statement helps you to close the file without explicitly closing it. This is the correct way to write a line to the file. The with statement is used to wrap the execution of a block with methods defined by a context manager.

Do not use os.linesep() function as a line terminator when writing files opened in text mode (the default); use a single “\n” instead of on all platforms.


If the file does not exist, the open() function will create a new file. If the file exists and you want to append the new content, then while creating a file, use the “a” mode.

Use the “w” to write with truncation.

with open('data.txt', 'a') as f:
    f.write('Welcome Playstation 5\n')


It will create a new file called data.txt with the following content.

Welcome Playstation 5

Alternatively, you can use the print() function instead of write() function.

with open('data.txt', 'a') as f:
    print("hey there", file=f)


It will create a new file called data.txt with the following content.

hi there

The write() function is more efficient then print() function. So my recommendation is to use the write() function to write the lines in the file.

Python write multiple lines to file.

To write multiple lines to a file in Python, use a with open() function and then the writelines() function.

The writelines() method writes the items of a list to the file. The texts will be inserted depending on the file mode and stream position.

Syntax of writelines()



list The list of texts or byte objects that will be inserted.



We will write three lines to a new file.

with open('data.txt', 'a') as f:
    line1 = "PS5 Restock India \n"
    line2 = "Xbox Series X Restock India \n"
    line3 = "Nintendo Switch Restock India"

    f.writelines([line1, line2, line3])


The output will be a data.txt file with the following content.

PS5 Restock India 
Xbox Series X Restock India 
Nintendo Switch Restock India

As you can see that we wrote three lines in a newly created file.

Python write line by line to csv

To write line by line to a csv file in Python, use either file.write() function or csv.writer() function. The csv.writer() function is used to create a writer object. 

We will see an example of a file.write() method.

txt = ["PS5", "is", "currently", "unavailable", "in", "India"]

with open('shows.csv','w') as file:
    for line in txt:


It will create the shows.csv file with the following content.


And we get the text inside the csv file.

Let’s see the csvwriter approach.

import csv

txt = ["PS5", "is", "currently", "unavailable", "in", "India"]

with open('shows.csv', 'w', newline='') as file:
    writer = csv.writer(file)



That is it for this tutorial.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.