Skip to content
Advertisement

What is the usage of 2nd parameter in os.execl()

This question is inspired by the comments in Why am I getting an “execv(file, args)” error when using execl()?.

When we use

os.execl(sys.executable, "python", "script.py")

The second parameter seems redundant for me, can anyone help explain why we need this parameter?

Advertisement

Answer

In the old good days without graphical monitors and without mice, a text-only console (or even a teletype) was the only user interface between a user and the operating system.

This interface (Command Line Interface, CLI) instead of nowadays GUI (Graphical User Interface) forced a user to write commands, including ones for launching executables, instead of clicking on their icons.

(Most present operating systems still have this possibility, e.g. command-line window or PowerShell in Windows, or console / terminal window in GNU/Linux.)

The command line for launching a program had / has a form

              program  argument1  argument2 ...

where program (in those old days were only “programs” and “programmers”, no “applications” and “developers”) was the name of the executable file, often including a relative/absolute path.

The operating system launched program, passing to it not only arguments (argument1, argument2, …), but — as so-called 0th argument — the program itself, too.
(In the same form, as it was typed, i.e. with or without a path, with or without a suffix / extension.)

So the launched program may found out, how it was launched. It may use this info for various purposes, for example

  • to forbid launch it with the other name (“nomen est omen”) — so if you renamed an executable, you had a bad luck,

  • to reach a different behavior in dependency of the launching name (some executables were / are installed with soft links with different names),

  • for deriving the location of the helper / user files from the path, if it was explicitly used in the command.

Many contemporary programs (oh no, applications) still exploit the possibility to launch them from CLI with arguments, but (mostly) ignore the name of the program.

For example, in Windows you may use the command

       notepad somename.txt

to launch the Notepad with the already opened somename.txt file. Or — to not go far away — Python may launch your script with the command

       python somescript.py

Back to the main question:

The os.execl() function mimics the behavior of launching an application from CLI, giving the user (of this function) the possibility to specify the program as he or she needs. It is the second parameter of the os.execl() function. As I mentioned, the behavior of some — mainly old — programs depends on it.


Addendum:

Python has in its sys.argv list all command-line parameters — including the 0th argument in its sys.argv[0] element.

But note — if you launch your script with the command

python  /somefolder/somescript.py  xxx  7

from the python itself point of view (not interesting for us) the arguments are:

  • the 0th argument is the string "python",
  • the 1st argument is the string "/somefolder/somescript.py",
  • the 2nd argument is the string "xxx",
  • the 3rd argument is the string "7",

but from the somescript.py point of view — i.e. what your script will obtain — the arguments are

  • the 0th argument is the string "/somefolder/somescript.py" — it is in sys.argv[0],
  • the 1st argument is the string "xxx" — it is in sys.argv[1],
  • the 2nd argument is the string "7" — it is in sys.argv[2].
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement