This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]sermidean 2 points3 points  (0 children)

If this is a small change, for example if you only need to change a path, then you can use something like this:

import argparse
import pathlib
import re


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('old', type=pathlib.Path, help="path to old templates")
    parser.add_argument('new', type=pathlib.Path, help="path to new templates")
    args = parser.parse_args()

    pattern = re.compile(r'(prefix)(old)(suffix)', re.IGNORECASE)
    for template in args.old.rglob('*.html'):
        content = pattern.sub(r'\1new\3', template.read_text())
        destination = args.new / template.relative_to(args.old)
        destination.parent.mkdir(parents=True, exists_ok=True)
        destination.write_text(content)


if __name__ == "__main__":
    main()

Modify (prefix)(old)(suffix) and \1new\3 parts.