Merge pull request #252 from mithro/only-change-on-contents

Only write file if contents will change.
This commit is contained in:
Tim Ansell 2019-09-02 14:42:22 -07:00 committed by GitHub
commit 27c334d440
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 2 deletions

View File

@ -28,6 +28,11 @@ def write_to_file(filename, contents, force_unix=False):
newline = None newline = None
if force_unix: if force_unix:
newline = "\n" newline = "\n"
old_contents = None
if os.path.exists(filename):
with open(filename, "r", newline=newline) as f:
old_contents = f.read()
if old_contents != contents:
with open(filename, "w", newline=newline) as f: with open(filename, "w", newline=newline) as f:
f.write(contents) f.write(contents)