- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
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).
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())
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).
- Related Articles
- How to write binary data to a file using Python?
- How to write into a file from command line using Python?
- How to write a single line in text file using Python?
- How to open a file to write in Python?
- Python - How to write pandas dataframe to a CSV file
- How to write an image to a file using OpenCV?
- How to Write/create a JSON file using Java?
- How to rename a file using Python?
- How to remove a file using Python?
- How to find a file using Python?
- How to delete a file using Python?
- Write a program in Python to export a given dataframe into Pickle file format and read the content from the Pickle file
- How to write multiple lines in text file using Python?
- How to write a text file in Selenium with python?