FreeLand
FreeLand

Reputation: 169

How to not read a csv file if its being written to in that instance?

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

Answers (1)

Ronald
Ronald

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

Related Questions