Executing a "dir" using PyRun_String??? URGENT!!!

Ben Held ben.held at staarinc.com
Mon Mar 19 08:55:14 EST 2001


I am trying to execute a dir command from C using PyRun_String where
pcObjectString is a module name imported using "import <module>".  The
problem is that PyRun_String always returns a NULL.  I have using code
similiar to this for calling other functions and they work, but I cannot get
this to work.  Any ideas?

Ben

  PyObject* pModule = PyImport_AddModule("__builtin__");
 PyObject* pDict = PyModule_GetDict(pModule);

  char pcCommand[1000];
  strcpy(pcCommand, "pSTADir = dir(\"");
  strcat(pcCommand, pcObjectString);
  strcat(pcCommand, "\")");
  PyObject* pyRunObject = 0;
  PyObject* pDir = 0;
  if (pyRunObject = PyRun_String(pcCommand, eval_input, pDict, pDict)) {
   pDir = PyDict_GetItemString(pDict, "pSTADir");
   if (!pDir) return false;
   int listsize = PyList_Size(pDir);
   PyObject* pItem = 0;
   CString strEntry;
   for (int i = 0; i < listsize; i++) {
    pItem = PyList_GetItem(pDir, i);
    strEntry = CString(PyString_AsString(pItem));
    strEntry.Remove('\r');
    if (strEntry[0] == '_') continue;
    dirEntries += strEntry + " ";
   }
   if (pDir) Py_DECREF(pDir);
  }
  if (pyRunObject) Py_DECREF(pyRunObject);






More information about the Python-list mailing list