Computer Science
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
Related Questions
Write a program that reads characters from the keyboard one by one. All lower case characters get stored inside the file LOWER, all upper case characters get stored inside the file UPPER and all other characters get stored inside file OTHERS.
Write a function in Python to count and display the number of lines starting with alphabet 'A' present in a text file "LINES.TXT". e.g., the file "LINES.TXT" contains the following lines:
A boy is playing there. There is a playground. An aeroplane is in the sky. Alphabets & numbers are allowed in password.
the function should display the output as 3.
Write a program that will create an object called filout for writing, associate it with the filename STRS.txt. The code should keep on writing strings to it as long as the user wants.
Consider the following definition of a dictionary Member, write a method in Python to write the content in a pickled file member.dat.
Member = {'MemberNo.': ..............., 'Name': ...............}