Python

3 Ways to Write a List to a File in Python

This quick tutorial demonstrates three methods to write a list to a file in Python scripting language.

Abhishek Prakash
Abhishek Prakash

In an earlier scripting tip, I showed you how to execute Shell commands in Python. In this quick tutorial, I’ll show you several ways for writing a list to a file in Python.

Writing a List to a File in Python

Actually the methods I am going to discuss here are used for writing text to a file in Python. The examples I am using here discusses writing the list to file but you can use it to write any kind of text. string etc using the functions mentioned here.

Method 1: Writing a list to a file line by line in Python using print

The print command in Python can be used to print the content of a list to a file. The list elements will be added in a new line in the output file.

Here’s a sample Python program for your reference:

MyList = ["New York", "London", "Paris", "New Delhi"]
MyFile=open('output.txt','w')

for element in MyList:
    print >>MyFile, element
MyFile.close()

The file output.txt is opened in writing mode. The for loop in python iterates at each element in the list. The print commands write the element to the opened file.

If you use the cat command to display the content of the output file, it will be this:

New York
London
Paris
New Delhi

As you can see, the list elements have been written line by line in the output file.

In the examples, the file has been opened in write mode with the ‘w’ option. All the previous content of the file in this mode will be overwritten.

If you want to save the previous content of the file, you can open the file in append mode by using the option ‘a’ instead of ‘w’.

Method 2: Write list to file in using Write function in Python

Now let’s see another way to save list to file in Python. We are going to use the Write() function.

The write() function takes string as the argument. So, we’ll be using the for loop again to iterate on each element in the list and write it to the file.

Here’s a sample Python program:

MyList = ["New York", "London", "Paris", "New Delhi"]
MyFile=open('output.txt','w')

for element in MyList:
     MyFile.write(element)
     MyFile.write('\n')
MyFile.close()

Note that the elements in the list don’t have new line character. This is why I added the new line character ‘\n’ after each iteration so that the list elements are written to the file line by line. If you don’t do that you’ll see all the list content in one single line.

Method 3: Write all the lines to a file at once using writelines() function

The two other methods we saw wrote the content of the list to the file in line by line fashion. writelines() method is different. This method takes a list as input and writes the list as it is to the opened file.

Here’s a simple program:

MyList = ["New York", "London", "Paris", "New Delhi"]

MyFile=open('output.txt','w')
MyFile.writelines(MyList)
MyFile.close()

If you use a command to view file in Linux command line, here’s what you’ll see in the output:

New YorkLondonParisNew Delhi

It’s not the prettiest output, I accept. But that’s the exact content of the list.

If you want to print line by line text to the file using writelines function, you can still do that but you’ll have to use extra effort.

What you can do here is that modify the list itself and add the newline character after each element. And now if you print the list using writelines(), you’ll have the output in line by line fashion.

Here’s the sample program for that:

MyList = ["New York", "London", "Paris", "New Delhi"]

MyFile=open('output.txt','w')
MyList=map(lambda x:x+'\n', MyList)
MyFile.writelines(MyList)
MyFile.close()

I hope this quick little tip helped you in writing a list to file using Python. If you use some other way, do share it with us. If the tutorial helped you, please let me know by leaving a comment below.



Join the conversation.