Ensure parent directory before writing to file