Add 'a' (append) mode to test condition before writing. 

Fixes a bug where a file opened in append mode is not allowed to write because mode 'a' is not part of the conditional test.