You don't close the file, so maybe the data is lost on exit. Try explicitly closing the file after you've written to it.