make sure the directory exists before writing to it