Writing and Reading List To A File In Python

Python list data structure is heavily used to store multiple items in a single variable. The Python list is very similar to the array in other programming languages. The popular use case for the list is storing the list and its items into persistent storage to read later. There are different ways where storing a list in a database is an option. But putting a list into a database can be costly and hard to implement. another way is serializing the list and storing it in a file. Simple the list is written into a file.

Write and Read List Into A File with write() and read() Methods

In order to write and read list into a file the file mehtods write() and read() can be used. In the following example we will create a list named countries and store countries. This list will be written into a file named “countries.txt”.

countries=['USA','Turkey','Germany']

with open('countries.txt', 'w') as file:
    for country in countries:
        file.write('%s\n' % country)

We can see that every item is stored in a separate line. So in this example, the list items are iterated one by one and every item is written into the file named “countries.txt” and in every item, the “\n” end of the line is added.

After a list is written into a file we generally need to read this file into a list. Reading a file into a list is very similar to the writing into a file.

countries=[]

with open('countries.txt', 'r') as filehandle:
    for line in file:
        countries.append(line[:-1])

Write and Read List Into A File with writelines() and readlines() Methods,

Python also provides the writelines() and readlines() methods in order to write and read multiple lines with a single statement. The writelines() can be used to write a list into a file. As the writelines() method write all provided data at once the end of the line should be provided for each list item.

countries=['USA','Turkey','Germany']

with open('countries.txt', 'w') as file:

    file.writelines('%s\n' % country for country in countries)

The file-written list can be read by using the readlines() method. The file named countries all lines read with the readlines() method and every line is iterated with them for a loop. Every line item is stored inside the list named countries.

# define empty list
countries= []

# open file and read the content in a list
with open('countries.txt', 'r') as file:
    countries = [country.rstrip() for country in file.readlines()]

Write and Read List Into A File with pickle

The pickle is a serialization and storage module provided by Python. The pickle module provides the dump() method in order to serialize the provided data and store it in a file. The pickle module serializes the data in binary format which means the written file can not be read like a text file. the pickle.dump() method accepts two parameters where the first one is the data we want to serialize and the second one is the filehandle we want to store. The filehandle should be already created and capable of write data.

import pickle

countries=['USA','Turkey','Germany']

with open('countries.data', 'wb') as file:
    pickle.dump(countries, file)

The python list stored file can be read with the pickle.load() method. The load() method of the pickle only accepts a single parameter which is the file we want to read.

import pickle

countries=[]

with open('countries.data', 'rb') as file:
    countries=pickle.load(file)

Leave a Comment