Class - 12 CBSE Computer Science Important File Handling Questions 2025
Write a program that counts the number of characters up to the first $ in a text file.
Python File Handling
2 Likes
Answer
Let the sample file "myfile.txt" contain the following text:
Hello world! This is a test file.
It contains some characters until the first $ is encountered.
count = 0
with open("myfile.txt", 'r') as file:
while True:
char = file.read(1)
if char == '$' or not char:
break
count += 1
print(count)
Output
78
Answered By
1 Like