I’m writing a python script that copies a file using shutil.copyfile() on Linux. During the copying, other processes may be trying to read the file. Is the following sufficient to ensure that an external process doesn’t get a corrupted view of the file?
os.unlink(dest) shutil.copyfile(src, dest)
That is, is shutil.copyfile() atomic such that other processes cannot read the destination file until after the copy operation is complete?
No, it seems to just loop, reading and writing 16KB at a time.
For an atomic copy operation, you should copy the file to a different location on the same filesystem, and then
os.rename() it to the desired location (which is guaranteed to be atomic on Linux).