November 13th, 2012, 07:27 AM
I'm looking for the simplest code to check if an imputed file contains an imputed string. If the the file does contain the sting, I would like the Python shell to print: 'Yes', and if not, to print: 'No'.
November 13th, 2012, 08:36 AM
Originally Posted by dannyz_au
with open(<input file name>) as f:
if <input string> in f.read(): print "yes"
else: print "no"
November 13th, 2012, 09:01 AM
print(('No','Yes')[goal in text_to_be_searched])
You can choose how you populate the goal and the text_to_be_searched strings. source is a file name.
>>> with open(source,'r') as pf:
... text_to_be_searched = pf.read()
>>> import sys
>>> goal = sys.stdin.read()[:-1]
>>> print(('No','Yes')[goal in text_to_be_searched])
[/code] are essential for python code and Makefiles!