Write a List to A File in Python

Write a list to a file in Python

Python allows us to work with different data structures and write them to an external file.

In this tutorial, we will learn how to write a list to a file in Python. If you want write list to CSV file, you can refer write list to CSV in Python.

Using the write() function to write a list to a file in Python

The write() function is used to write contents to a file in Python. We use the open() function to create a file object and open it in write mode. We can iterate through the list and write every element individually.

We implement this in the following code.

Output
Here is output of the program.
Write list to file Python output

You need to change r'Desktop\Manav\Article\sample1.txt' to the path where you want to write list.

The %s is used to specify that the item is a string and \n adds a new line after every element.

Instead of iterating through the list, we can also use the join() function. The join() function will combine all the elements of the list after separating them with a specified character.

For example,

When we open a file in write mode (using w), it clears the contents of the given file. If a file contains some contents and we want to preserve it then we should open the file in append mode.

See the code below.

This way, we start writing the data to the list to the end of the file, without clearing any data which is present earlier.

Using the writelines() function to write a list to a file in Python

The writelines() function is used to write the contents of a list to a file. We can open the file using the open() function in the required mode.

We can open the file in write or append mode in this function.

For example,

Using the pickle.dump() function to write a list to a file in Python

The pickle module is used to serialize and deserialize objects in Python by converting them to bytes. For this, we use the dump() function in this module. We need to open the file using the open() function as we did in the earlier method.

For example,

Using the json.dump() function to write a list to a file in Python

The dump() function from the json module works similarly to the one discussed earlier.

See the following code.

Using the unpack operator * to write a list to a file in Python

The unpack operator can be used to unpack an iterable object. In Python 3.5 and above we can use the print() function with the unpack operator to write a list to a file in Python.

We implement this below.

Using the numpy.savetxt() function to write a list to a file in Python

The savetxt() function can be used to export an array or a list to an external text file. We can specify the format for the string using the fmt parameter, newline character, and even set the delimiter character using the delimiter argument.

We use this function in the following example.

That’s all aboout how to write a list to a file in python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *