title: Iterate through multiple files and append text from HTML using Beautiful Soup
body: <p>I have a directory of downloaded HTML files (46 of them) and I am attempting to iterate through each of them, read their contents, strip the HTML, and append only the text into a text file. However, I'm unsure where I'm messing up, though, as nothing gets written to my text file?</p>
  
  <pre><code>import os
  import glob
  from bs4 import BeautifulSoup
  path = "/"
  for infile in glob.glob(os.path.join(path, "*.html")):
          markup = (path)
          soup = BeautifulSoup(markup)
          with open("example.txt", "a") as myfile:
                  myfile.write(soup)
                  f.close()
  </code></pre>
  
  <p>-----update----
  I've updated my code as below, however the text file still doesn't get created.</p>
  
  <pre><code>import os
  import glob
  from bs4 import BeautifulSoup
  path = "/"
  for infile in glob.glob(os.path.join(path, "*.html")):
      markup = (infile)
      soup = BeautifulSoup(markup)
      with open("example.txt", "a") as myfile:
          myfile.write(soup)
          myfile.close()
  </code></pre>
  
  <p>-----update 2-----</p>
  
  <p>Ah, I caught that I had my directory incorrect, so now I have:</p>
  
  <pre><code>import os
  import glob
  from bs4 import BeautifulSoup
  
  path = "c:\\users\\me\\downloads\\"
  
  for infile in glob.glob(os.path.join(path, "*.html")):
      markup = (infile)
      soup = BeautifulSoup(markup)
      with open("example.txt", "a") as myfile:
          myfile.write(soup)
          myfile.close()
  </code></pre>
  
  <p>When this is executed, I get this error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:\Users\Me\Downloads\bsoup.py, line 11 in &lt;module&gt;
      myfile.write(soup)
  TypeError: must be str, not BeautifulSoup
  </code></pre>
  
  <p>I fixed this last error by changing</p>
  
  <pre><code>myfile.write(soup)
  </code></pre>
  
  <p>to</p>
  
  <pre><code>myfile.write(soup.get_text())
  </code></pre>
  
  <p>-----update 3 ----</p>
  
  <p>It's working properly now, here's the working code:</p>
  
  <pre><code>import os
  import glob
  from bs4 import BeautifulSoup
  
  path = "c:\\users\\me\\downloads\\"
  
  for infile in glob.glob(os.path.join(path, "*.html")):
      markup = (infile)
      soup = BeautifulSoup(open(markup, "r").read())
      with open("example.txt", "a") as myfile:
          myfile.write(soup.get_text())
          myfile.close()
  </code></pre>