I want to obtain the true value of the operating system's argv[0] in a Python program. Python's sys.argv[0] is not this value: it is the name of the Python script being executed (with some exceptions). What I want is a foo.py that will print "somestring" when executed as
exec -a "somestring" python foo.py
The trivial program
#! /usr/bin/env python
import sys
print sys.argv[0]
will print "foo.py" instead.
Does anyone know how to obtain this? There are some related functions in the Python C API: e.g. Py_GetProgramName. But this doesn't seem to be exposed to the Python world anywhere. Py_GetProgramFullPath works off of argv[0] but munges it try to obtain a path to a Python interpreter. (This value is propagated to sys.executable, so that variable isn't right either.) Do I really have to write a C module to get this value?
Edit: Also asked (but not helpfully answered) here.
On Linux you can read the contents of /proc/self/cmdline
:
#!/usr/bin/env python
import sys
print sys.argv[0]
f = open('/proc/self/cmdline', 'rb')
cmdline = f.read()
f.close()
print repr(cmdline.split('\x00'))
And the output is:
$ bash
$ exec -a "somestring" python foo.py
foo.py
['somestring', 'foo.py', '']
There seems to be a bug in The exec command replaces the shell closing the terminal session after the interpreter exits. That's the first bash for.bash
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With