How to start a python program recursive with os.execv?
Question: I have the following lines:

    
    
    #!/root/p34/bin/python 
    import os
    import sys
    
    for i in range(10):
        print(i)
    
    currentFile = os.path.abspath(__file__)
    print(currentFile)  
    os.execv(currentFile, sys.argv)
    

When I try to run from console(Ubuntu 14.04) ./restart.py I get:

    
    
    ': [Errno 2] No such file or directory'
    

When I run `/root/p34/bin/python restart.py` I get python error:

    
    
    Traceback (most recent call last):
      File "restart.py", line 10, in <module>
        os.execv(currentFile, sys.argv)
    FileNotFoundError: [Errno 2] No such file or directory
    

Can anyone help me with this problem?


Answer: `os.execv` does not look for shebang lines; that is a shell function instead.

Use
[`sys.executable`](https://docs.python.org/2/library/sys.html#sys.executable)
to get the path to the current Python binary:

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

