execl difficulty

Bengt Richter bokr at oz.net
Sun Dec 14 19:44:48 EST 2003


On Mon, 15 Dec 2003 00:18:32 +0100, "Fredrik Lundh" <fredrik at pythonware.com> wrote:

>"python newbie" wrote:
>
>> Can anyone tell me why this would cause "Invalid Number of Parameters" while
>> trying to compile (or interpret or whatever)
>>
>> import os
>> os.execl("C:/WINDOWS/system32/xcopy.exe","E:/MainWeb/dreampics/*.*","E:/Main
>> Web/dp")
>>
>> Here's what the Python reference says:
>>      execl(path,arg0, arg1, ...)
>
>first, fix the slashes.  xcopy wants backward slashes in the filenames,
>not forward slashes.
>
>next, fix the number of arguments.  at the top of the reference page
>you refer to, there's a paragraph explaining how the exec arguments
>work:
>
>    The various exec*() functions take a list of arguments for the
>    new program loaded into the process. In each case, the first
>    of these arguments is passed to the new program as its own
>    name rather than as an argument a user may have typed on
>    a command line. For the C programmer, this is the argv[0]
>    passed to a program's main(). For example, "os.execv('/bin/echo',
>    ['foo', 'bar'])" will only print "bar" on standard output;
>    "foo" will seem to be ignored.
>
>    (from http://www.python.org/doc/current/lib/os-process.html )
>
>in other words, you need to pass in the program name twice, to make
>sure xcopy sees the filenames as argument 1 and 2.
>
Hmph, I missed that, in spite of the fact that I execl'd my C++ args printer:

Like OP's xcopy execl:

 >>> os.execl(r'c:\util\pargs.exe', '1', '2', '3')
 argc    =  3
 argv[0] = "1"
 argv[1] = "2"
 argv[2] = "3"

from command line, arg[0] gets inserted:

 [16:35] C:\pywk\clp>c:\util\pargs.exe 1 2 3
 argc    =  4
 argv[0] = "c:\util\pargs.exe"
 argv[1] = "1"
 argv[2] = "2"
 argv[3] = "3"

where

[16:58] E:\VCWK\test\t3>type pargs.cpp
#include <stdio.h>
int main(const int argc,const char *argv[]){
   printf("argc    = %2d\n",argc);
   for(int i=0;i<argc;i++){
      printf("argv[%d] = \"%s\"\n",i,argv[i]);
   }
   return 0;
}

Too hurried. Anyway, the above illustrates what you point out.

>(are you sure you want os.exec, btw?  os.system or os.spawn* might
>be a better choice...)
Seems like.

Regards,
Bengt Richter




More information about the Python-list mailing list