Python Write List to file
This article provides a comprehensive guide on writing a list to file in Python using several examples,
How to Write a List to File in Python?
The write() and read() methods are used in Python to read and write text into a file. Consider the following steps for writing a list to a file in Python.
Step 1: Use the open() Function to Open the File in Write Mode
The open() function is used in Python to open a file. The open() function takes the file name or complete location along with access mode (w) as an argument and opens the file. The specified access mode allows us to open a file in write mode.
Step 2: Initializing List and Iterating it Using For Loop
The for loop is used to loop/iterate each item in a specified list. We process the list sequentially to save each item in a file.
for ele in list1:
Step 3: Adding Item Into File Using write() Method
Now inside the for loop, the write() method is used to write each element of the specified list to the given file. Here is the code:
file1.write(ele + '\n')
Step 4: Closing File After Entering List of Items
Once we complete the process of entering items into a file, make sure your file is now closed. The close() function is used to make the file close.
Complete Code
Here is the complete Python code for implementing all the steps above:
list1 = ["Java", "Python", "C++"]
for ele in list1:
file1.write(ele + '\n')
file1.close()
The above code iterates the list’s each element one by one and writes it to the directed file.
Output
Take a look at other examples of writing lists to files in Python:
Example 1: Write a List Into a File in Python
First, we are going to make a list of names like names = [‘Ace’, ‘jen’, ‘stella’]. After that, we open a file using the open() function by directing the file’s path into parenthesis. We are using for loop for the iteration of names.
names = ['Ace', 'jen', 'stella']
# open file in write mode
with open(r'C:\Users\Kashif Javed\Documents\filename.txt', 'w') as fp:
for item in names:
# write each item on a new line
fp.write("%s\n" % item)
print('Done')
Output
Example 2: Read the Same List Back from a File in Python
Let’s check into memory back the same list by using r which is used for reading mode. The specified file is opened in r mode using the open() function and iterated using the for loop. For each iteration, the items/elements have been appended to the given list. Here is an example code:
names = []
# open the file and read the content in a list
with open(r'C:\Users\Kashif Javed\Documents\filename.txt', 'r') as fp:
for line in fp:
# remove linebreak from a current name
# linebreak is the last character of each line
x = line[:-1]
# Add the current items to the list
names.append(x)
# display list
print(names)
Output
Conclusion
This article presents a guide on how to write a list to a file in Python using the write() function and a for loop. It covers the steps of opening the file, iterating over the list of elements, and writing each element to the file. The article also includes examples of writing a list into a file and reading the list back from the file. Overall, it provides a comprehensive approach to handling file operations with lists in Python.
Source: linuxhint.com