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.

Output :

Red                                                                     
Green                                                                   
White                                                                   
Black                                                                   
Pink                                                                    
Yellow

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

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



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program