You keep trying to open the file, and with your conditional it would be not true as it will see the file as allready open and thow an error back.

Either open the file at the beginning, or close the file within the function call.
_________________________
Today is the tomorrow you worried about yesterday.