How to get the caller script name

后端 未结 4 1222
天命终不由人
天命终不由人 2020-12-19 14:44

I\'m using Python 2.7.6 and I have two scripts:

outer.py

import sys
import os

print \"Outer file launching...\"
os.system(\'inner.py\')
4条回答
  •  -上瘾入骨i
    2020-12-19 15:15

    On linux you can get the process id and then the caller name like so.

    p1.py

    import os
    os.system('python p2.py')
    

    p2.py

    import os
    
    pid = os.getppid()
    cmd = open('/proc/%d/cmdline' % (pid,)).read()
    caller = ' '.join(cmd.split(' ')[1:])
    print caller
    

    running python p1.py will yield p1.py I imagine you can do similar things in other OS as well.

提交回复
热议问题