What does Python\'s string.replace return if no string substitution was made? Does Python\'s file.open(f, \'w\') always touch the file even if no changes were made?
What does Python's string.replace return if no string substitution was made?
str.replace() returns the string itself or a copy if the object is a subclass of string.
Does Python's file.open(f, 'w') always touch the file even if no changes were made?
open(f, 'w') opens and truncates the file f.
Note the code below is CPython specific; it won't work correctly on pypy, jython:
count = 0
for match in all_files('*.html', '.'):
content = open(match).read()
replacedText = content.replace(oldtext, newtext)
if replacedText is not content:
count += 1
open(match, 'w').write(replacedText)
print (count)