def write_data(fpath, data, modified, raise_err=True, tmp_dir=None):
"""Safely write data to file path.
"""
tmp_dir = tmp_dir or os.path.dirname(fpath)
with tempfile.NamedTemporaryFile(dir=tmp_dir,
delete=False,
prefix='.tmp') as temp:
if data:
temp.write(data)
os.fchmod(temp.fileno(), 0o644)
os.utime(temp.name, (modified, modified))
try:
os.rename(temp.name, fpath)
except OSError:
_LOGGER.error('Unable to rename: %s => %s', temp.name, fpath,
exc_info=True)
if raise_err:
raise
评论列表
文章目录