擅长:python、mysql、java
<p>自Python 3.3以来,现在有了一个标准的跨平台解决方案,<a href="https://docs.python.org/3/library/os.html#os.replace" rel="noreferrer">^{<cd1>}</a>:</p>
<blockquote>
<p>Rename the
file or directory src to dst. If dst is a directory, <code>OSError</code> will be
raised. <strong>If dst exists and is a file, it will be replaced silently if
the user has permission</strong>. The operation may fail if src and dst are on
different filesystems. If successful, the renaming will be an atomic
operation (this is a POSIX requirement).</p>
<p><strong>Availability: Unix, Windows.</strong></p>
<p>New in version 3.3.</p>
</blockquote>
<p>然而,与文档相反,在Windows上它不能保证是原子的(在Python 3.4.4中)。这是因为<a href="https://hg.python.org/cpython/file/v3.4.4/Modules/posixmodule.c#l4289" rel="noreferrer">internally</a>它在Windows上使用<a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa365240%28v=vs.85%29.aspx" rel="noreferrer">^{<cd2>}</a>,这并不能保证。</p>