Skip to content

Instantly share code, notes, and snippets.

@tapan-p
Created October 3, 2019 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tapan-p/e0a62886f3a5250d6a549c2a8d5ea95a to your computer and use it in GitHub Desktop.
Save tapan-p/e0a62886f3a5250d6a549c2a8d5ea95a to your computer and use it in GitHub Desktop.
Search string in file
#File being in same directory as current program
with open('filename.txt') as f:
lines = f.read().splitlines()
print(len(lines))
matching = [s for s in lines if "SearchString" in s] #return the list of lines with SearchString in it
if len(matching)==0:
print "Not found"
else:
print matching
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment