Insert HTML tag in all HTML files in a folder in Python
<p>I'm new to python and trying a program to do the following:</p>

<ol>
<li><p>Open all folder and subfolders in a directory path</p></li>
<li><p>Identify the HTML files</p></li>
<li><p>Load the HTML in BeautifulSoup</p></li>
<li><p>Find the first body tag</p></li>
<li><p>If the body tag is immediately followed by &lt; Google Tag Manager> then continue</p></li>
<li><p>If not then add &lt; Google Tag Manager> code and save the file.</p></li>
</ol>

<p>I'm not able to scan all subfolders within each folder.
I'm not able to set seen() if &lt; Google Tag Manager> appears immediately after the body tag.
Any help to perform the above tasks is appreciated.</p>

<p>My code attempt is as follows:</p>

<pre><code>  import sys
  import os
  from os import path
  from bs4 import BeautifulSoup

  directory_path = '/input'

  files = [x for x in os.listdir(directory_path) if path.isfile(directory_path+os.sep+x)]

  for root, dirs, files in os.walk(directory_path):
      for fname in files:
      seen = set()
      a = directory_path+os.sep+fname
      if fname.endswith(".html"):
          with open(a) as f:
              soup = BeautifulSoup(f)
              for li in soup.select('body'):
                if li in seen:
                    continue
                else:
                    seen.add("&lt;!-- Google Tag Manager --&gt;&lt;noscript&gt;&lt;iframe src='//www.googletagmanager.com/ns.html?id=GTM-54QWZ8'height='0' width='0' style='display:none;visibility:hidden'&gt;&lt;/iframe&gt;&lt;/noscript&gt;&lt;script&gt;(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], j=d.createElement(s),dl=l!='dataLayer'?'&amp;l='+l:'';j.async=true;j.src='//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);})(window,document,'script','dataLayer','GTM-54QWZ8');&lt;/script&gt;&lt;!-- End Google Tag Manager â&gt;\n")
</code></pre>

<p>So you can install the <a href="https://docs.python.org/3/library/glob.html" rel="nofollow">iglob</a> library for python. With iglob you can recursively traverse the main directory you specify and the sub-directories and list all the files with a given extension. Then open up the HTML file, read all the lines, traverse through the lines manually until you find "" for the tag as some users who may use a frame work might have other content inside the body tag. Either way, loop through the lines looking for the start of the body tag, then check the next line, if the text as you specified "Google Tag Manager" is not in the next line, write it out. Please keep in mind I wrote this in the event you will always have the Google Tag Manager tags right after the body tag. </p>

<p>Please keep in mind that:</p>

<ol>
<li>In the event the Google Tag Manager text is not directly after the body tag, this code will add it anyways, so if Google Tag manager is somewhere in the Two body tags, and works, this could break the functionality of your Google Tag Manager.</li>
<li>I am using Python 3.x for this, so if you are using Python 2, you might have to translate this to that version of python. </li>
<li>Replace the 'Path.html' with the variable path so that it rewrite the file it is looking at with the modifications. I put  in 'path.html' so that i could see the output and compare to original while I was writing the script.</li>
</ol>

<p>Here is the code:</p>

<pre><code>import glob

types = ('*.html', '*.htm') 
paths = []

for fType in types:
    for filename in glob.iglob('./**/' + fType, recursive=True):
        paths.append(filename)
#print(paths)

for path in paths:
    print(path)
    with open(path,'r') as f:
        lines = f.readlines()

    with open(path, 'w') as w:
        for i in range(0,len(lines)):
            w.write(lines[i])
            if "&lt;body&gt;" in lines[i]:
                if "&lt;!-- Google Tag Manager --&gt;" not in lines[i+1]:
                    w.write('&lt;!-- Google Tag Manager --&gt; &lt;!-- End Google Tag Manager --&gt;\n')
</code></pre>

python html beautifulsoup