重启后如何returnpython?

How to return after restarting python?

代码如下:

def test():
    //restarting python
    os.execv(sys.executable, [sys.executable] + sys.argv)
print("Successfull")
return("Succesfull")

它正在重新启动 python 但没有返回或打印值。

我认为你的缩进不正确所以请看下面的代码:

def test():
    try:
        print("Successfull")
        return("Succesfull")
    except Exception:
        #do something
    finally:
        os.execv(sys.executable, [sys.executable] + sys.argv)


test()

Note: The finally statement will be executed even if an exception was caught.