简体   繁体   中英

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM