Created
June 22, 2018 22:48
-
-
Save lanbugs/960798fad6fc79a505aa38576f1000bb to your computer and use it in GitHub Desktop.
Search in file and get line number with python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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