消息 [106587]
os.rename() is atomic on Linux, but on Windows it raises an error if the destination does already exist.
Not atomic pseudo-code for Windows:
if exists(b):
unlink(b)
rename(a, b)
Windows offers different functions depending on the version:
- MoveFileTransacted(): atomic! version >= (Windows Vista, Windows Server 2008)
- ReplaceFile(): version >= Windows 2000
- MoveFileEx() with MOVEFILE_REPLACE_EXISTING and MOVEFILE_WRITE_THROUGH flags: not atomic (eg. "If the file is to be moved to a different volume, the function simulates the move by using the CopyFile and DeleteFile functions."), version >= Windows 2000
I don't think that it's possible to write an atomic rename (file) function for any OS, so it's only a "best effort" atomic function. The documentation will give a list of OS on which the operation *is* atomic (eg. Linux).
Note: os.rename() uses MoveFileW() on Windows. |
|
| 日期 |
用户 |
动作 |
参数 |
| 2010-05-26 23:41:42 | vstinner | 修改 | recipients:
+ vstinner |
| 2010-05-26 23:41:41 | vstinner | 修改 | messageid: <1274917301.93.0.503310941815.issue8828@psf.upfronthosting.co.za> |
| 2010-05-26 23:41:40 | vstinner | 链接 | issue8828 messages |
| 2010-05-26 23:41:40 | vstinner | 创建 | |
|