title: how to find a string in a text file and print the lines right before and after
body: <p>I am trying to convert my shell scripts into python code, but I am stuck while trying this operation.</p>
  
  <p>I have a process that outputs a text file, the file has sections like this:</p>
  
  <pre><code>Running Operation kdasdakdnaskdaksdma
  
   (error if present) error: kdmakmdasmdaksom
  </code></pre>
  
  <p>This file could have multiple lines for the operation and the error (if present, otherwise the next line will just have another operation); there is always a crlf after each block.</p>
  
  <p>I am trying to scan the file to find the line that contains "error:", and then read the operation that caused the error and the details of the error, so i can extrapolate it from the text file and save it in an error log file.</p>
  
  <p>So far i can find the line(s) that has "error:" in it, with this simple code, but I am not able to find any example about how do you actually print the lines that are not necessarily the ones that contain the error message, but the ones that came before and after the line where "error:" is located.</p>
  
  <p>using awk or grep would be straightforward, but with Python I am not really sure about how to do so; this is what i have so far, that is able to print the line that has the error but it prints just that, while i would like to have control to the lines printed before and after.</p>
  
  <pre><code>import re
  
  fh = open('~/logs_output.txt')
  
  for line in fh:
      if "error:" in line:
          print line
  </code></pre>
  
  <p>Tried to look at RE module in python, and also to the string modules, but so far I haven't found anything that would allow me to do what you would do with awk for example, where you can  look for an occurrence of a specific string and turn on print, and then turn it off, once that you are done</p>
  
  <p>Can anyone point me to the right direction to tackle this issue? Thanks!</p>