Run multiple find and replaces in Python (on every file in the folder and sub-folder)
Question: I have a folder (courses) with sub-folders and a random number of files. I
want to run multiple search and replaces on those random files. Is it possible
to do a wild card search for `.html` and have the replaces run on every html
file ?

Search and replaces:

* 1) `"</b>"` to `"</strong>"`
* 2) `"</a>"` to `"</h>"`
* 3) `"<p>"` to `"</p>"`
* Also all these replaces have to be run on every file in the folder and sub-folders.

Thank you so much


Answer: Try this,

    
    
    import os
    from os.path import walk
    
    mydict = {"</b>":"</strong>", "</a>":"</h>", "<p>":"</p>"}
    
    for (path, dirs, files) in os.walk('./'):
        for f in files:
            if f.endswith('.html'):
                filepath = os.path.join(path,f)
                s = open(filepath).read()
                for k, v in mydict.iteritems():
                    s = s.replace(k, v)
                f = open(filepath, 'w')
                f.write(s)
                f.close()
    

You can change `os.walk('./')` to `os.walk('/anyFolder/')`

