def atomic_write(filename, content, overwrite=True, permissions=<NUM_LIT>, encoding='<STR_LIT:utf-8>'):
filename = os.path.expanduser(filename)<EOL>if not overwrite and os.path.exists(filename):<EOL><INDENT>raise WriteError('<STR_LIT>'.format(filename))<EOL><DEDENT>dirname = os.path.dirname(filename)<EOL>with tf.NamedTemporaryFile(dir=dirname, prefix='<STR_LIT:.>', delete=False) as tmp:<EOL><INDENT>if isinstance(content, six.string_types):<EOL><INDENT>tmp.write(content.decode(encoding))<EOL><DEDENT>else:<EOL><INDENT>tmp.write(content)<EOL><DEDENT><DEDENT>os.chmod(tmp.name, permissions)<EOL>os.rename(tmp.name, filename)<EOL>
Write a file atomically by writing the file content to a
temporary location first, then renaming the file. 

TODO: this relies pretty heavily on os.rename to ensure atomicity, but 
os.rename does not silently overwrite files that already exist on 
Windows natively. For now, the functionality provided here can only be 
supported under Windows Subsystem for Linux on Windows 10 version 1607 
and later.

:param filename: Filename
:type filename: str
:param content: File content
:type content: str
:param overwrite: Overwrite
:type overwrite: bool
:param permissions: Octal permissions
:type permissions: octal