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