Opening and Reading a File |Section 5| Celestial Warrior

1 year ago
1

Please download the text file from the attachment.

Then write some code that reads the content of the file andgenerates the following output in the command line:

pear
apple
orange
mandarin
watermelon
pomegranate

file = open("fruits.txt", "r")
content = file.read()
file.close()
print(content)

Please modify thecode of the previous exerciseso that instead of printing out thelines in the terminal, it prints out the length of each line.

Expected output:

4
5
6
8
10
11

file = open("fruits.txt", "r")
content = file.readlines()
file.close()
for i in content:
print(len(i.strip()))

Loading comments...