Reputation: 169
Is this something that can be done in python or any language? Is there a way to detect if a csv file is being written to in that instantaneous moment?
Upvotes: 0
Views: 217
Reputation: 1039
So you want to update the CSV file atomically. Starting to write over the existing file as you have realized is not atomic and will get you in trouble.
The trick is to write the new data to a temporary new file and then move the temp file over the live file. The move operation is atomic (for practical purposes).
create-new-csv-data > new-data.csv
mv new-data.csv data.csv
For probably more info than you want to know about how atomic a mv
really is, see for example https://unix.stackexchange.com/questions/322038/is-mv-atomic-on-my-fs.
Upvotes: 1