Python File I/O: Write a List content to a file
12. Write List to File
Write a Python program to write a list content to a file.
Sample Solution:-
Python Code:
color = ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']
with open('abc.txt', "w") as myfile:
for c in color:
myfile.write("%s\n" % c)
content = open('abc.txt')
print(content.read())
Sample Output:
Red Green White Black Pink Yellow
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python program to write a list of strings to a file, each on a new line, and then read the file to verify.
- Write a Python program to write a list to a file and then append the current timestamp after writing.
- Write a Python function to write a list of numbers to a file (one per line) and then compute the sum of the numbers from the file.
- Write a Python program to write a list of strings to a file and then count the total number of characters written.
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to get the file size of a plain file.
Next: Write a Python program to copy the contents of a file to another file .
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.