count = 0
for match in all_files('*.html', '.'):
    with open(match) as thefile:
        content = thefile.read()
replacedText = content.replace(oldtext, newtext)
if replacedText != content:
    with open(match, 'w') as thefile:
        count += 1
thefile.write(replacedText)
print(count)
Python string replace in a file without touching the file if no substitution was made