Fix: write to file should handle bytes

The previous code uses 'r' which I believe is a typo