Filename has to be encoded according to the system’s expected filename encoding before passing filenames to C library functions.
If there is already a existing
PyObject * that needs to be converted as a filename, then use the code given below :
If a filename is to be returned back to Python, use the following code given below –
Code #3 :
Dealing with filenames in a portable way is a tricky problem that is best left to Python. Filenames will be handled in a manner that, if one uses the above in extension C code.
Passing Open Files to C Extensions –
Code #4 : To convert a file to an integer file descriptor, use
The resulting file descriptor is obtained by calling the
fileno() method on fobj. Thus, any object that exposes a descriptor in this manner should work (e.g., file, socket, etc.). A descriptor can be passed to various low-level C functions that expect to work with files.
PyFile_FromFd() is used to convert an integer file descriptor back into a Python object.
Code #5 :
The arguments to
PyFile_FromFd() mirror those of the built-in open() function. NULL values simply indicate that the default settings for the encoding, errors, and newline arguments are being used.
- C Extension Module using Python
- C API from Extension Module in Python | Set 2
- C API from Extension Module in Python | Set 1
- Python | Extension function operating on Arrays
- Python | Opaque Pointers in C extension modules
- Python | Repeat and Multiply list extension
- Python | Sort and store files with same extension
- Passing function as an argument in Python
- Python | Passing dictionary as keyword arguments
- Unicode Strings Passing to C Libraries
- Passing NULL-Terminated Strings to C Libraries
- Important differences between Python 2.x and Python 3.x with examples
- Python | Convert list to Python array
- Python | Index of Non-Zero elements in Python list
- Python | Merge Python key values to list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.