I am using Python to write chunks of text to files in a single operation:
open(file, 'w').write(text)
If the script is interrupted so a file write does not complete I want to have no file rather than a partially complete file. Can this be done?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Write data to a temporary file and when data has been successfully written, rename the file to the correct destination file e.g
According to doc http://docs.python.org/library/os.html#os.replace
Note:
It may not be atomic operation if src and dest locations are not on same filesystem
os.fsyncstep may be skipped if performance/responsiveness is more important than the data integrity in cases like power failure, system crash etc