I am trying to count number of occurrences of a word from a file, using Python. But I have to ignore comments in the file.
I have a function like this:
def getWordCount(file_name, word):
count = file_name.read().count(word)
file_name.seek(0)
return count
How to ignore where the line begins with a #
?
I know this can be done by reading the file line by line like stated in this question. Are there any faster, more Pythonian way to do so ?