2

I am trying to run the same program recursively but with a different argument. I am doing it like this:

os.execv(file_dir, ['python'] + [sys.argv[0]] + [str(last_line)])
quit()

This is a snippet of a function that i call from my main function. I tried making sure that that file is executable by doing chmod u+x program.py but that did not work. What can the issue be?

giulio di zio
  • 171
  • 1
  • 11
  • 1
    while you might get a response explaining how to do it, that seems like an antipattern. It is way better to write a function taking these parameters and call it as a function – Marat Jul 02 '20 at 14:21

1 Answers1

2

os.execv expects the full path to the executable as the first argument, not a "file directory".

Try this: os.execv(sys.executable, ['python'] + [sys.argv[0]] + [str(last_line)])

Example yes implementation by recalling the same executable:

import sys
import os

print('y')
os.execv(sys.executable, ['python'] + [sys.argv[0]])
orestisf
  • 1,396
  • 1
  • 15
  • 30
  • If I do that I get ```can't open file 'program.py': [Errno 2] No such file or directory``` – giulio di zio Jul 02 '20 at 14:22
  • That's probably because in the original call argv[0] is a relative path. Try `os.path.abspath(sys.argv[0])` – orestisf Jul 02 '20 at 14:23
  • BTW I tried printing out what sys.executable is and it is in usr/bin/python3 while my project containing the file I want to execute is in a folder on my Desktop – giulio di zio Jul 02 '20 at 14:23
  • Ok ye you were right with your last comment. thank you so much. Now I am having another issue with import statements in the recursive subprogram but I will post another question for that – giulio di zio Jul 02 '20 at 14:30