check if output dir exists before attempting to write, fixes segfault.