title: Python Memory error during function return statement
body: <p>Hi i am processing a 600Mb file. i have written the below code. What i am doing was, to search for a keyword in the data between  <code>&lt;dest&gt;</code> tags and if it exists then add a city tag to <code>&lt;dest&gt;</code> tag. It worked fine for small set of data but when i ran the program on large file it is throwing MEMORY ERROR. I guess i am getting this error when i use return statement in <code>if condition</code> can any one please let me know how to solve this?</p>
  
  <pre><code>import re
  
  def casp ( tx ):
      def tbcnv( st ):
          ct = ''
          prt = re.compile(r"(?i)(Slip Copy,.*?\))", re.DOTALL|re.M)
          val = re.search(prt, st)
          try:
              ct = val.group(1)
              if re.search(r"(?i)alaska", ct):
                  jval = "Alaska"
                  print jval
                  if jval:
                      prt = re.compile(r"(?i)(.*?&lt;dest.*?&gt;)", re.DOTALL|re.M)
                      vl = re.sub(prt, "\\1\n" +  "&lt;city&gt;" + jval + "&lt;/city&gt;" + "\n" ,st)
                      return vl
                  else:
                      return st
              else:
                  return st
          except:
              print "Not available"
              return st   
  
      pt = re.compile("(?i)(&lt;dest.*?&lt;/dest&gt;)", re.DOTALL|re.M)
      t = re.sub(pt, lambda m: tbcnv(m.group(1)), tx)
      return t
  
  with open('input.txt', 'r') as content_file:
      content = content_file.read()
      pt = re.compile(r"(?i)&lt;Lrlevel level='3'&gt;(.*?)&lt;/Lrlevel&gt;", re.DOTALL|re.M)
      content = re.sub(pt,lambda m: "&lt;Lrlevel level='3'&gt;" + casp(m.group(1) + "&lt;/Lrlevel&gt;" ), content)
  
  with open('out.txt', 'w') as out_file:
      out_file.write(content)
  </code></pre>