My simple script to check for a certain word in files seems to be failing, and I cannot seem to explain it through documentation or searching. The code is below. I believe I have narrowed it down to the ‘in’ operator failing by printing the code itself and finding the word that I am looking for. If curious, this script is to find certain keywords in the Quake source code, since I’d rather not look through 30+ full source files. Any help would be appreciated, thanks!
import os
def searchFile(fileName, word):
file = open(os.getcwd() + "\\" + fileName,'r')
text = file.readlines()
#Debug Code
print text
if(word in text):
print 'Yep!'
else:
print 'Nope!'
The reason it is failing is because you are checking if the word is within the text’s lines. Just use the read() method and check in there or iterate through all the lines and each each individually.