How can you obtain the OS's argv[0] (not sys.argv[0]) in Python?

后端 未结 1 1181
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-12 00:41

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 ex

相关标签:
1条回答
  • 2021-01-12 00:43

    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 bash The exec command replaces the shell closing the terminal session after the interpreter exits. That's the first bash for.

    0 讨论(0)
提交回复
热议问题