Wednesday, August 9, 2023

Q1)Given a file called myfile.txt which contains the 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.

 # Read the content from the file

with open("myfile.txt", "r") as file:

    content = file.read()

 

# Split the content into words

words = content.split()

 

# Write each word in a separate line to a new file

with open("transformed_file.txt", "w") as new_file:

    for word in words:

        new_file.write(word + "\n")

 

print("Content transformed and saved to transformed_file.txt")

No comments:

Post a Comment