Make sure directory exists before writing to it