Changed open mode to 'a+'
Also create directory path if not exist