Exercise 41

Write a python program that takes as input a given list and creates a text file whose lines are exactly the elements of this list. Example: for the list

List_programming_books =  ["Python programming course", "Java programming course", "C ++ programming course", "C # programming course"]

, the generated file will be formed by the lines:
Python programming course
Java programming course
C ++ programming course
C # programming course

Solution

# define the input list
List_programming_books = ["Python programming course", "Java programming course", "C ++ programming course", "C # programming course"]
#open a file in write mode using the with open statement
with open('programming_books.txt', 'w') as file:

    # iterating over all list items
    for book in List_programming_books:
        # write the item 'book' as line in programming_books.txt
        file.write(book + '\n')





 

Younes Derfoufi
my-courses.net

One thought on “Solution Exercise 41: python program which convert a list to text file”

Leave a Reply