title: Python: Loop through multiple text files, extract target strings, and append them to list
body: <p>(Python 3.6.3)</p>
  
  <p>I need to extract IP addresses and the dates from multiple text files that contain a long text string. After this, I want to append these data to the Python lists. These text files also reside in sub-directories so I used 'os.path.join(subdir, file) to make sure the script captures these as well.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>ip_address = []
  dates = []
  
  for subdir, dirs, files in os.walk(rootdir):
      for file in files:
          if file.endswith('.txt'):
              text_data = open(os.path.join(subdir, file))
              ip_address.append(re.findall( r'[0-9]+(?:\.[0-9]+){3}', text_data))
              dates.append(re.findall( r'[0-9]+(?:\/[0-9]+){2}', text_data))
          else:
              pass
  </code></pre>
  
  <p>However, I get the following error:</p>
  
  <pre><code>TypeError                                 Traceback (most recent call last)
  &lt;ipython-input-28-de806e6c6270&gt; in &lt;module&gt;()
        6         if file.endswith('.txt'):
        7             text_data = open(os.path.join(subdir, file))
  ----&gt; 8             ip_address.append(re.findall( r'[0-9]+(?:\.[0-9]+){3}', text_data))
        9             dates.append(re.findall( r'[0-9]+(?:\/[0-9]+){2}', text_data))
       10         else:
  
  C:\Users\591159\AppData\Local\Continuum\Anaconda3\lib\re.py in findall(pattern, string, flags)
      220 
      221     Empty matches are included in the result."""
  --&gt; 222     return _compile(pattern, flags).findall(string)
      223 
      224 def finditer(pattern, string, flags=0):
  
  TypeError: expected string or bytes-like object
  </code></pre>
  
  <p>I assume the data I am trying to extract is not in a string form but don't fully understand what it means. I appreciate any pointer to the right direction. Thank you!</p>