Skip to content

Instantly share code, notes, and snippets.

@lanbugs
Created June 22, 2018 22:48
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 lanbugs/960798fad6fc79a505aa38576f1000bb to your computer and use it in GitHub Desktop.
Save lanbugs/960798fad6fc79a505aa38576f1000bb to your computer and use it in GitHub Desktop.
Search in file and get line number with python
#!/usr/bin/env python
filename = 'test.txt'
search = 'foobar'
with open(filename) as f:
for num, line in enumerate(f, 1):
if search in line:
print '%s - found at line:' % search, num
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment