简体   繁体   中英

Share file stream between Python and C

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM