My txt file has:
/aaa/AB01_Z
/bbb/AB02_Z
/ccc/AB03_Z
/ddd/AB04_Z
Patterns of my interest are:
AB01, AB03
I would like to print lines with the pattern in this order.
I tried:
import re
lst = ['AB01', 'AB03']
f = open('myfile.txt')
for pattern in lst:
for line in f:
if re.search(pattern, line):
print(line)
However, this only returns '/aaa/AB01_Z'.
If I open the file under the loop for the pattern list, I get '/aaa/AB01_Z' and '/ccc/AB03_Z' as expected:
for pattern in lst:
f = open('myfile.txt')
for line in f:
if re.search(pattern, line):
print(line)
Why does not the loop for pattern run in the first case?