Python脚本调用其他Python脚本

时间:2013-10-20 23:59:28

标签: python python-2.7

我需要创建一个脚本来调用特定目录中的每个.py文件,允许被调用的脚本访问有关调用脚本的信息(类,变量等)。

所以我有这样的事情:

mainfile.py:

class MainClass:
    def __init__(self):
        self.myVar = "a variable"

        for f in os.listdir(path):
            if f.endswith(".py"):
                execfile(path+f)

    def callMe(self):
        print self.myVar

myMain = MainClass()
myMain.callMe()

我希望能够在callee.py中执行以下操作

myMain.callMe()

1 个答案:

答案 0 :(得分:0)

如何使用os.sytem

for f in os.listdir(path):
    if f.endswith(".py"):
        os.system(path+f)
相关问题