Exercise 57

Given a file called myfile.txt which contains the following text:
"Python is object oriented programming language".
Write a program in Python that transforms the content of the file by writing each word in a separate line.

Solution

# opening file in read mode
f = open("myfile.txt" , 'r')

# getting the file content
content = f.read()

# converting the content to a list
L = content.split()
f.close()

# opening file in write mode by overwriting on its content
f = open("myfile.txt" , 'w')

# building the contnent file
for word in L:
f.write(word + "n")
f.close()
Younes Derfoufi
my-courses.net

Leave a Reply