title: Have python loop through log files and find string from each one
body: <p>How can I get Python to loop through a directory and find a specific string in each file located within that directory, then output a summary of what it found?</p>
  
  <p>I want to search the long files for the power status (HOST_POWER), which is either "ON" or "OFF".</p>
  
  <pre><code>&lt;GET_HOST_POWER
      HOST_POWER="ON"
      /&gt;
  </code></pre>
  
  <p><strong>Here is what I have so far:</strong></p>
  
  <pre><code>import glob
  import os
  
  print("The following list contains the power status of each server.\n")
  
  os.chdir( "LOGS\\" )
  for file in glob.glob('*.log'):
      with open(file) as f:
          contents = f.read()
      if 'HOST_POWER="ON"' in contents:
          print (file + " = ON")
      if 'HOST_POWER="OFF"' in contents:
          print (file + " = OFF")
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>The following list contains the power status of each server.
  
  server1.web.com.log = ON
  server2.web.com.log = ON
  server3.web.com.log = ON
  server4.web.com.log = OFF
  server5.web.com.log = ON
  </code></pre>
  
  <p>This is working perfectly now, except how can I remove the ".log" from each line so it makes more sense to other users?</p>
  
  <hr>
  
  <p><strong>More info about what I'm trying to do:</strong></p>
  
  <p>I have a series of log files located here:</p>
  
  <p>LOGS\</p>
  
  <p>In the "logs" directory, I have a bunch of log files like the following:</p>
  
  <pre><code>server1.web.com.log
  server2.web.com.log
  server3.web.com.log
  server4.web.com.log
  server5.web.com.log
  </code></pre>
  
  <p>Each log file contains information like the following:</p>
  
  <pre><code>IP Address is: server1.web.com &lt;?xml version="1.0"?&gt; &lt;RIBCL VERSION="2.22"&gt; &lt;RESPONSE
      STATUS="0x0000"
      MESSAGE='No error'
       /&gt; &lt;/RIBCL&gt; &lt;?xml version="1.0"?&gt; &lt;RIBCL VERSION="2.22"&gt; &lt;RESPONSE
      STATUS="0x0000"
      MESSAGE='No error'
       /&gt; &lt;/RIBCL&gt; &lt;?xml version="1.0"?&gt; &lt;RIBCL VERSION="2.22"&gt; &lt;RESPONSE
      STATUS="0x0000"
      MESSAGE='No error'
       /&gt; &lt;/RIBCL&gt; &lt;?xml version="1.0"?&gt; &lt;RIBCL VERSION="2.22"&gt; &lt;RESPONSE
      STATUS="0x0000"
      MESSAGE='No error'
       /&gt; &lt;GET_HOST_POWER
      HOST_POWER="ON"
      /&gt; &lt;/RIBCL&gt; &lt;?xml version="1.0"?&gt; &lt;RIBCL VERSION="2.22"&gt; &lt;RESPONSE
      STATUS="0x0000"
      MESSAGE='No error'
       /&gt; &lt;/RIBCL&gt; &lt;?xml version="1.0"?&gt; &lt;RIBCL VERSION="2.22"&gt; &lt;RESPONSE
      STATUS="0x0000"
      MESSAGE='No error'
       /&gt; &lt;/RIBCL&gt; &lt;?xml version="1.0"?&gt; &lt;RIBCL VERSION="2.22"&gt; &lt;RESPONSE
      STATUS="0x0000"
      MESSAGE='No error'
       /&gt; &lt;/RIBCL&gt;
  
  iLO_config_utility\cpqlocfg.exe: Script succeeded on "server1.web.com:443"
  </code></pre>
  
  <p>I want my Python code to loop through each log file and find the status of the host power:</p>
  
  <pre><code>&lt;GET_HOST_POWER
      HOST_POWER="ON"
      /&gt;
  </code></pre>
  
  <p>It could be either "HOST_POWER="ON" or "HOST_POWER="OFF"</p>
  
  <p>Then compile a summary like the following:</p>
  
  <pre><code>server1.web.com = ON
  server2.web.com = ON
  server3.web.com = OFF
  server4.web.com = ON
  server5.web.com = ON
  </code></pre>