You might want to take a step back. Why are you opening and closing the file so much. There's a lot of overhead to do that all the time. It is probably better to have it opened once and keep that handle available for later use.
If you still want to do what you are doing, then you need to give us a clearer understanding of what you are attempting to do. Writing an example to show this would probably help.