Computer Science

Write the output of the following code with justification if the contents of the file "VIVO.txt" are:

"Welcome to Python Programming!"

F1 = open("VIVO.txt", "r")
size = len(F1.read())
print(size) 
data = F1.read(5)
print(data)

Python

Python File Handling

2 Likes

Answer

The given code opens a file named "VIVO.txt" in read mode and reads its entire content into a variable F1. It then calculates the length of this content using len() and prints the result, which is 30 characters long. However, when it tries to read the next 5 characters from the file using F1.read(5), no characters are left to read since the file pointer is at the end the file. As a result, data remains an empty string.

Output
30

Answered By

2 Likes


Related Questions