You have tried to access a file without opening it first.
Resolution:
Open the file with the open mode that you need and try the operation again. As this error implies that your program logic contains a mistake, you might want to terminate the run and recode your program.