You can do this the old (new?) Way by simply calling the io module.
This code works, but error checking fails. See Documents for an explanation.
PyObject *ioMod, *openedFile; PyGILState_STATE gilState = PyGILState_Ensure(); ioMod = PyImport_ImportModule("io"); openedFile = PyObject_CallMethod(ioMod, "open", "ss", "foo.txt", "wb"); Py_DECREF(ioMod); PyObject_CallMethod(openedFile, "write", "y", "Written from Python C API!\n"); PyObject_CallMethod(openedFile, "flush", NULL); PyObject_CallMethod(openedFile, "close", NULL); Py_DECREF(openedFile); PyGILState_Release(gilState); Py_Finalize();
source share