Search code examples
pythonreturn-valuerestart

How to return after restarting python?


Here is code:

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

It's restarting python but not returning or printing value.


Solution

  • I think your indentation is not correct so please have look at beloow code:

    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.