What is an alternative to execfile in Python 3?

前端 未结 12 1966
滥情空心
滥情空心 2020-11-22 02:33

It seems they canceled in Python 3 all the easy way to quickly load a script by removing execfile()

Is there an obvious alternative I\'m missing?

12条回答
  •  余生分开走
    2020-11-22 03:10

    You could write your own function:

    def xfile(afile, globalz=None, localz=None):
        with open(afile, "r") as fh:
            exec(fh.read(), globalz, localz)
    

    If you really needed to...

提交回复
热议问题