mraps98

Python_countWordInFile

May 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.34 KB | None | 0 0
  1. def countWord(text,word):
  2.     count = int()
  3.     words = text.split()
  4.     for each in words:
  5.         if each == word:
  6.             count += 1
  7.     return count
  8.  
  9. filename = input("Enter file to analyze: ")
  10. char = input("What word do you want to count?: ")
  11.  
  12. with open(filename, 'r') as fi:
  13.     text = fi.read()
  14.     print(countWord(text,char))
Add Comment
Please, Sign In to add comment