I am learning File Handling in python. I tried to write a program to display the number of lines starting with 'H' and the total number of words in the file. Even though the text file is not empty, it only printed 0 for both. What's wrong here?
with open("para.txt","a+") as f:
f.write("As I said: not all things should be left up to god.")
lines = 0
# to display the number of lines starting with H
l = f.readlines()
for i in l:
if i[0] == "H":
lines += 1
print("No. of lines starting with H is", lines)
#to display the total number of words in the file
data = f.read()
split_data = data.split()
count = 0
for i in split_data:
count += 1
print("Total number of words:", count)
print(f.tell())