Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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