Rename the
file or directory src to dst. If dst is a directory, OSError will be
raised. If dst exists and is a file, it will be replaced silently if
the user has permission. 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).
你可以试试^{} :
或者
os.remove
然后shutil.move
:正如documentation所说,如果文件存在,就不可能保证在Windows上执行原子重命名操作,所以Python所做的是要求自己执行两步
os.remove
+os.rename
,处理潜在的错误。在unix系统上,
rename
覆盖目标(如果存在)(因为该操作保证是原子的)。请注意,在windows上,删除目标文件也可能失败,即使您有权限,因为该文件可能正在使用中。这是windows文件系统的另一个基本限制,您必须在代码中自己处理它。
自Python 3.3以来,现在有了一个标准的跨平台解决方案,^{} :
然而,与文档相反,在Windows上它不能保证是原子的(在Python 3.4.4中)。这是因为internally它在Windows上使用^{} ,这并不能保证。
相关问题 更多 >
编程相关推荐