Create directories before attempting to write files