2

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.