Latest Code Tutorials

How to Write Line to File in Python


Python has various methods for creating, reading, writing, updating, and deleting files. Python allows users to handle files, i.e., to read and write files, along with 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 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, then 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 in it.

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 in it.

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. Where the texts will be inserted depends on the file mode and stream position.

Syntax of writelines()



listThe 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.