test.txt in der Gesucht wird nach foobar:
wer w erw erwer foobar sfsdfhsdkjfhkjsdf sdf sdf sdf sdf sdf sdflskdjflsdjflksjflksjf sdfkjsdfjkhskjhffoobardjskfhskdjhfkjsdhfkjshdf sflksdjfjklsdfjs dfs dfs df sdf sdf dsf
Testscript zum Suchen:
#!/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
Resultat:
dev1@cmkdev1:/home/dev1$ python test.py foobar - found at line: 5 foobar - found at line: 13