I've been trying to integrate C code into Python under Linux and I came up with the following problem: ¿is it possible to share an already opened file between C and Python? I mean a C FILE and a Python file object.
The C function which I'm struggling with is called exhaustively, so I'd like to avoid opening/closing the file each time this happens and pass the opened file from Python to C. I'm open to any efficient solution.
It should be possible. In C, you can get the file descriptor with fileno(fh) and open it in Python with os.fdopen(fd). Make sure you remember to close it -- I doubt that the Python file object going out of scope would accomplish this.
Share file stream between Python and C