title: How to extract specific emails from a text file
body: <p>New to python and stuck on this! </p>
  
  <p>I have a large text file of just emails from various domains. Say I want to pull out just gmail AND hotmail. </p>
  
  <p>I was able to with this but it comes out all mixed together. </p>
  
  <pre><code>filename = input('Enter filename to open: ')
  try:
      filename = open(filename)
  except:
      print('File cannot be opened: ', filename)
  exit()
  import re
  for line in filename:
      line = line.rstrip()
      x = re.findall('\S+@gmail.com',  line)
      if len(x) &gt; 0:
          print(x)
      y = re.findall('\S+@hotmail.com',  line)
      if len(y) &gt; 0:
          print(y)
  </code></pre>
  
  <p>My goal is to have it print out all the gmail emails and then list the number of gmail email addresses it found. Then I would like it to list all the hotmail accounts and list how many hotmail accounts it found. Make sense? </p>