重启python后如何返回?

重启python后如何返回?,python,return-value,restart,Python,Return Value,Restart,以下是代码: def test(): //restarting python os.execv(sys.executable, [sys.executable] + sys.argv) print("Successfull") return("Succesfull") 它正在重新启动python,但没有返回或打印值。我认为您的缩进不正确,请查看下面的代码: def test(): try: print("Successfull") ret

以下是代码:

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()
注意:即使捕获到异常,也将执行
finally
语句


return
print
应该位于从不调用的函数
test()
中。你到底想在这里做什么?我在类中有一个名为restart()的函数,我需要重新启动python,并将状态代码重新返回为200,然后打印“successfull”消息。这看起来像是在执行os.execv(sys.executable,[sys.executable]+sys.argv)@rts之前如何返回“successull”的问题。你为什么还要返回运行execv前是否“成功”?