-4

I tried this but it does not work:

c=0

file_path ="file.txt"

with open (file_path) as file_object :

 

    for line in file_object:

        print(len(line))

        for word in line :

            if word == "Python":

                c+= 1
print(c)
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • Does this answer your question? [Efficiently count word frequencies in python](https://stackoverflow.com/questions/35857519/efficiently-count-word-frequencies-in-python) – Shunya Jul 03 '22 at 09:22

1 Answers1

0

This could be a possible solution:

def count_words(file_path: str, expected_word: str) -> int:
    count = 0

    with open(file_path, 'r') as f:
       for line in f.readline():
        for word in line.split(" "):
            if word == expected_word:
                count += 1

occurences = count_words("file.txt", "Python")
print(occurences)
lukasl-dev
  • 724
  • 3
  • 11