How to Write a List Content to a File using Python?


In this article, we will show you how to write the data present in the list into a text file using python.

Assume we have taken a list and written all the elements of the list into a text file say "ListDataFile.txt" which has the following data after writing into it.

Let’s say that we have following list in a text file −

inputList = ['This', 'is', 'a', 'TutorialsPoint', 'sample', 'file']

we will get the following result of the above string with this program −

This
is
a
TutorialsPoint
sample
file

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the list of elements.

  • Create a variable to store the path of the text file.

  • Use the open() function(opens a file and returns a file object as a result) to open the text file in read-only mode by passing the file name, and mode as arguments to it (Here “w” represents write mode).

with open(inputFile, 'w') as filedata:
  • Traverse through each element of the input list using the for loop.

  • Write each element of the list(iterator value) into the opened text file using the write() function(writes a specified text to a file. The provided text will be inserted depending on the file mode and stream position)

  • Traverse in each line of the file using the for loop.

  • Close the input file with the close() function(used to close an opened file).

  • Use the open() function(opens a file and returns a file object as a result) to open the text file in read-only mode by passing the file name, and mode as arguments to it (Here “r” represents read-only mode).

with open(inputFile, 'r') as filedata:
  • Print the contents of the text file after reading the file data using the read() function(reads the specified number of bytes from the file and returns them. The default value is -1, which means the entire file).

Example

The following program traverse lines of a text file and prints the frequency of key-value pair from a text file using the counter function from the collections module −

# input list inputList = ['This', 'is', 'a', 'TutorialsPoint', 'sample', 'file'] # input text file inputFile = "ListDataFile.txt" # Opening the given file in write mode with open(inputFile, 'w') as filedata: # Traverse in each element of the input list for item in inputList: # Writing each element of the list into the file # Here “%s\n” % syntax is used to move to the next line after adding an item to the file. filedata.write("%s\n" % item) # Closing the input file filedata.close() # Opening the output text file in read-only mode fileData = open("ListDataFile.txt") # Reading the file and printing the result print(fileData.read())

Output

On executing, the above program will generate the following output −

This
is
a
TutorialsPoint
sample
file

We read a list of words in this program, then took a file and opened it in write mode. Then, using a loop, we traversed the list for each word and added that word to the file using the write() function. We used the newline character (\n) to separate the file's words. After that, we closed the file and reopened it in reading mode, printing all of the file's data (Here the data will be the words of the list)

We learnt how to open a file in writing mode and write data into it, as well as how to traverse the list of words and copy those items to the file, from this article. How to Reopen a File in order to Read Its Content (This is used to check whether the words are added to the file or not).

Updated on: 17-Aug-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements