Home > Software design >  How can you do batch find and replace across multiple HTML files using Python?
How can you do batch find and replace across multiple HTML files using Python?

Time:09-23

I frequently have folders of 10-20 HTML files that need to be updated. Right now, I'm opening each one up in a text editor and using find and replace 5-10 times per file.

How can I search all 20 files for X, and if X is found, replace it with Y?

CodePudding user response:

A simple solution would be to first create a list of all the locations of the files you're trying to change, using glob for example:

import os
import glob
base_directory = os.path.join('your', 'path', 'here')
all_html_files = glob.glob(base_directory   '*.html', recursive=True)

Now with that list, you can simply iterate through it and operate on each file individually:

for file in all_html_files:

    with open(file, 'r') as f:
        raw_file = f.read()
    
    # do your replacing here    
    processed_file = raw_file.replace('foo', 'bar')

    with open(file, 'w') as f:
        f.write(processed_file)

CodePudding user response:

If you are using python3.4 or newer you might harness pathlib built-in module for this task as follows

import pathlib
for p in pathlib.Path("<path_to_your_dir_here>").rglob("*.html"):
    text = p.read_text()
    text = text.replace("old", "new")
    p.write_text(text)
  • Related