Search code examples
pythonpython-os

Incorrect path to my file when I try to restart it


I am trying to execute this:

os.execv(sys.executable, ['python'] + sys.argv)

The result I am getting:

C:\Program Files\Python310\python.exe: can't find '__main__' module in 'c:\\Users\\user\\OneDrive\\Рабочий'

As you can see, the path after the 'module in' words is improper. I printed sys.argv and got:

c:/Users/user/OneDrive/Рабочий стол/myfolder/file.py


Solution

  • This is how to os.execv into another program passed in the command line:

    # as execv_argv.py
    import os
    import sys
    
    os.execv(sys.executable, [sys.executable] + sys.argv[1:])
    

    and call with

    python execv_argv.py "second exec.py"
    

    Note os.execv with full sys.argv will call the parent again forever.