开发者

How to do atomic file replacement?

开发者 https://www.devze.com 2023-04-11 02:50 出处:网络
What\'s the recommended way to replace a file atomically in Python? i.e. if the Python script is interrupted, there is a power outage etc. files 开发者_运维技巧do not have a high probability of ending

What's the recommended way to replace a file atomically in Python?

i.e. if the Python script is interrupted, there is a power outage etc. files 开发者_运维技巧do not have a high probability of ending up in an inconsistent state (half written to the disk).

A solution for Linux/UNIX platforms is preferred.

(I know getting 100% atomic operations might depend on your file system, but at least make the chances for the corruption low.)


Create a new file and os.rename() it over the existing file. This is atomic on most platforms under most conditions.

0

精彩评论

暂无评论...
验证码 换一张
取 消