Counting sentences in a file via sentence ending punctuation

I am trying to write code that reads text from a text file, and counts the number of sentences in the text via counting the number of ‘sentence ending’ punctuation (.?!). I initially had issues with the code counting extra full stops (such as those in B.Sc). I solved this by making the code only count full stops with a space after them, however this is now not counting the full stops that end a paragraph. e.g:

This is a sentence.
# that full stop isn't being counted.
This is another sentence.

This is my code so far:

def count_possible_sentences(file_name):
    file=open(file_name)
    text=file.read()
    file.close()
    numfs=0
    numem=0
    numqm=0
    numfs=text.count(". ")
    print(numfs)
    numem=text.count("!")
    print(numem)
    numqm=text.count("?")
    print(numqm)
    count=numfs+numem+numqm
    print(count)

I appreciate any help.

The full stops at the end of lines aren’t followed by a space, but by a newline "\n".

You might also want to check in case there’s a full stop at the end of the text without a final newline after it, like in "This is the last sentence.".

Thanks, thats helpful. Any suggestions on how to go about getting that final full stop counted? I know I could brute force it to work with the examples the question is asking, but that wouldn’t necessarily work for every case.

You could use the .endswith method.