Make sure directories exist before writing to them