w3resource logo
Python Exercises

Python File I/O: Write a List content to a file

Python File I/O: Exercise-12 with Solution

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

Python Code Editor:

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())

Improve this sample solution and post your code through Disqus