Write a python function to count the occurrence of a given word in a given file
Topic: Write a python function to count the occurrence of a given word in a given file
Solution
def check_word_count(word, file): if not os.path.isfile(file): raise FileNotFoundError if not isinstance(word, str): raise TypeError with open(file, 'r') as f: lines = f.readlines() words = [l.strip().split(' ') for l in lines] words = [word for sublist in words for word in sublist] c = Counter(words) return c.get(word, 0)
List all Python Programs