You can't use any Python functions or objects within a nogil block. If you want to do file IO with nogil, you'll have to do it with C. This blog post can probably help you. Specifically, you can cimport the usual C functions from stdio. These functions should be usable within nogil blocks. The below is from the blog post to which I linked above:
from libc.stdio cimport *
cdef extern from "stdio.h":
FILE *fopen(const char *, const char *)
int fclose(FILE *)
ssize_t getline(char **, size_t *, FILE *)