Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do atomic file replacement? [duplicate]

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.)

like image 543
Mikko Ohtamaa Avatar asked Oct 04 '11 08:10

Mikko Ohtamaa


People also ask

Is renaming a file Atomic?

Atomic renameThe rename function from the C library in Windows does not implement the POSIX atomic behaviour; instead it fails if the destination file already exists. However, other calls in the Windows API do implement the atomic behaviour.

How do you replace a file in Python?

replace() method in Python is used to rename the file or directory. If destination is a directory, OSError will be raised. If the destination exists and is a file, it will be replaced without error if the action performing user has permission.

Is moving a file an atomic operation?

For any individual file, the move or rename performed by mv is atomic provided that the file is moved within the same filesystem.

What is atomic file update?

An atomic operation is one that changes a system from one state to another without visibly passing through any intermediate states. Atomicity is desirable when altering the content of a file because: The process performing the alteration may fail or be stopped, leaving the file in an incomplete or inconsistent state.


1 Answers

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

like image 120
Sjoerd Avatar answered Sep 22 '22 13:09

Sjoerd