Why must path be in args in os.execv?

Michael P. Reilly Michael.P..Reilly at p98.f112.n480.z2.fidonet.org
Thu Jul 1 11:17:40 EDT 1999


From: "Michael P. Reilly" <arcege at shore.net>

Holger Jannsen <holger at phoenix-edv.netzservice.de> wrote:
: High,

: there's again something I don't understand. Hope you do.;-)

: I'd like to start a windows-executable with some arguments.
: Ok. 'os.execv' or anything like that is the thing.

: But if I don't repeat the path with the arguments, the execution fails.
: Better: Something happens, but not the correct execution.

: For testing I constructed an easy batch named test.bat with only one command:
: echo %0 %1 %2 %3 %4 %5 >result.log

: Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
: Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
:>>> import os
:>>> os.chdir("c:\\mypython")
:>>> os.execv("test.bat",["test.bat","1","2","3","4"])

: When I start with:
:>>> os.execv("test.bat",["1","2","3","4"])
: only the interpreter ends. No Log-File has been written...

: I really spent a long time since I found out. Perhaps the documentation
: have to be changed?
:>>>
: execv (path, args) 
: Execute the executable path with argument list args, replacing the current
process (i.e.,
: the Python interpreter). The argument list may be a tuple or list of strings.
: Availability: Unix, Windows. 
:>>>

os.execv is a construct originating on UNIX, where the program name
does not always reflect the program file's name (with reason).  It is
common to create an executable that can react differently when called
with a different name.  As a simple example, gzip, gunzip and gzcat are
all the same physical file on a UNIX box, but called with those names,
it will compress, uncompress and uncompress-and-stream-output,
respectively.

Including the program name in the execv call allows this from a calling
process.

But in most cases, you can just use the program file:
  def myexec(program, *args):
    if len(args) == 1 and type(args[0]) == type(args):
      args = args[0]
    os.execv(program, (program,) + args)

This places the program filename in the arg0 position (see section 6.1.5
"Process Management" in the Python Library Reference.

  -Arcege




More information about the Python-list mailing list