How to count word "test" in file on Python? -


i have file consists of many strings. looks like

sdfsdf sdfsdfsdf sdfsdfsdf test gggg uff test test fffffffff sdgsdgsdgsdg sdgsdgsdgsdg uuuttt 555555555 ddfdfdfff dddd4444 66677565 sdfsdf5 556e4ergferg ergdgdfgtest kdfgdfgfg test

how count words "test". tried, have result

f = open("file") words =  0 s in f:     = s.find('test')     if > -1:         words += 1 print(words) f.close() 

and script counts strings contain word "test". how count words?

if want find matches:

with open("file") f:     numtest = f.read().count("test") 

if want find word matches:

with open("file") f:     numtest = f.read().split().count("test") 

Comments