Writing to a file in Python inserts null bytes

Take the hint. Don't do this.

In the olden days (30 years ago -- seriously) we "updated" files with complex add/change/delete logic.

Nowadays, life is simpler if you write programs that

  1. Read the entire file into memory.

  2. Work on the objects in memory.

  3. Write the objects to a file periodically and when the user wants to save.

It's faster and simpler. Use pickle to dump your objects to a file. Don't mess with "records" or any attempt to change a file "in place".

If you really think you need SQL capabilities (Insert, Update, Delete) then use SQLite. It's more reliable than what you're attempting to do.


From the Python manual:

file.truncate([size])
Truncate the file's size. If the optional size argument is present, the file is truncated to (at most) that size. The size defaults to the current position. The current file position is not changed. Note that if a specified size exceeds the file’s current size, the result is platform-dependent: possibilities include that the file may remain unchanged, increase to the specified size as if zero-filled, or increase to the specified size with undefined new content. Availability: Windows, many Unix variants.

You are truncating the file and then writing item1 and item2 at the former end of the file. Everything before that ends up padded with 0 bytes.

f.seek(0)

Call this to reset the file position after the truncate.


It looks to me like you're forgetting to rewind your file stream. After f.truncate(0), add f.seek(0). Otherwise, I think your next write will try to start at the position from which you left off, filling in null bytes on its way there.

(Notice that the number of null characters in your example equals the number of characters in your deleted lines plus a carriage-return and line-feed character for each one.)