如何获得呼叫者脚本名称 [英] How to get the caller script name
本文介绍了如何获得呼叫者脚本名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Python 2.7.6,并且有两个脚本:
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')
调用inner.py:
calling inner.py:
import sys
import os
print "[CALLER GOES HERE]"
我希望第二个脚本(inner.py)打印调用者脚本的名称(outer.py). 我无法将带有第一个脚本名称的参数传递给inner.py,因为我有大量的被调用/调用者脚本,而且我无法重构所有代码.
I want the second script (inner.py) to print the name of the caller script (outer.py). I can't pass to inner.py a parameter with the name of the first script because I have tons of called/caller scripts and I can't refactor all the code.
有什么主意吗?
推荐答案
一个想法是使用psutil.
One idea is to use psutil.
#!env/bin/python
import psutil
me = psutil.Process()
parent = psutil.Process(me.ppid())
grandparent = psutil.Process(parent.ppid())
print grandparent.cmdline()
这当然取决于您如何启动outer.py. 此解决方案与操作系统无关.
This is ofcourse dependant of how you start outer.py. This solution is os independant.
这篇关于如何获得呼叫者脚本名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文