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 API from Extension Module in Python | Set 1
- C Extension Module using Python
- C API from Extension Module in Python | Set 2
- Python - Incremental List Extension
- Python | Sort and store files with same extension
- Python | Extension function operating on Arrays
- Python | Opaque Pointers in C extension modules
- Python | Repeat and Multiply list 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
- Python | Convert list to Python array
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.