def writeFileAtomically(text, path, encoding="utf-8"):
"""
Write text into a file at path. Do this sort of atomically
making it harder to cause corrupt files. This also checks to see
if text matches the text that is already in the file at path.
If so, the file is not rewritten so that the modification date
is preserved. An encoding may be passed if needed.
"""
if os.path.exists(path):
with open(path, "r", encoding=encoding) as f:
oldText = f.read()
if text == oldText:
return
# if the text is empty, remove the existing file
if not text:
os.remove(path)
if text:
with open(path, "w", encoding=encoding) as f:
f.write(text)
评论列表
文章目录