PyObject *obj; /* Object with the filename */
PyObject *bytes;
char *filename;
Py_ssize_t len;
bytes = PyUnicode_EncodeFSDefault(obj);
PyBytes_AsStringAndSize(bytes, &filename, &len);
/* Use filename */
//...
/* Cleanup */
Py_DECREF(bytes);
If you need to return a filename back to Python, use the following code:
/* Turn a filename into a Python object */
char *filename; /* Already set */
int filename_len; /* Already set */
PyObject *obj = PyUnicode_DecodeFSDefaultAndSize(filename, filename_len);