title: How to avoid file corruption in Python?
body: <p>I have a pretty basic question, perhaps I didn't know the right keywords as I couldn't find a previous answer. I use Python scripts control and gather information for a smarthome environment. I mostly use text files to store and update information within and between the scripts. However, I frequently run into this one issue whenever the server crashes or loses power: The file contents tend to corrupt or vanish while the crash happens.</p>
  <p>To write file content, I usually use a structure like this:</p>
  <pre><code>try:
      with open(savefile, &quot;r&quot;) as file:
          lines = file.readlines()
  except:
      lines = []
      pass
  lines.append(str(time.time()) + &quot;;&quot; + str(value) + &quot;\n&quot;)
  if len(lines) &gt; MAX_READINGS:
      lines = lines[-MAX_READINGS:]
  with open(savefile, &quot;w&quot;) as file:
      file.writelines(lines)
  </code></pre>
  <p>In case of partial corruption such as blank lines between the data points, I often use a line-by-line loop that only qualifies lines with the correct structure (such as a timestamp in the beginning in the example-like cases). However, sometimes a file gets corrupted to the point it only contains spaces or is empty, getting useless for the scrips depending on the data.</p>
  <p>The filesystem's integrity remains intact in crashes, so it's probably not a lower level problem. But what's the suggested workaround to minimize the corruption risk?</p>
  <p>Should I use the &quot;a&quot; mode to append a new line and have another way to deal with the file lengths (the MAX_READINGS), or should I make a temporary copy which I'd then use to overwrite the original after the writing is done? Or might there be an external library providing the right functionality?</p>