Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is python's shutil.move() atomic on linux?

I am wondering whether python's shutil.move is atomic on linux ? Is the behavior different if the source and destination files are on two different partitions or is it same as when they are present on the same partition ?

I am more concerned to know whether the shutil.move is atomic if the source and destination files are on the same partition !

like image 605
Kisalay Avatar asked Sep 15 '10 09:09

Kisalay


People also ask

Is Shutil move Atomic?

With shutil. move(), you can copy across file systems, but there's no guarantee of atomicity. If the operation is interrupted halfway through, you could end up with a half-written file at the destination.

What is Shutil move in Python?

move() method Recursively moves a file or directory (source) to another location (destination) and returns the destination. If the destination directory already exists then src is moved inside that directory. If the destination already exists but is not a directory then it may be overwritten depending on os.

Is CP Command Atomic?

There is no way to do this; file copy operations are never atomic and there is no way to make them.


1 Answers

It is not atomic if the files are on different filsystems. In that case, python opens the source and destination file, loops on reading from the source and writing to the desination and finally unlinks the source file.

If the source and destination file are on the same file system, python uses the rename() C call, which is atomic.

like image 161
nos Avatar answered Oct 12 '22 13:10

nos