Creates directories in the path to the output file if necessary