1

I have been coding a program that opens files and edits them, but for the code to do what needs to be done it has to auto reset itself, I've spent a while browsing the interwebs for some help but can't seem to find anyone else asking this question. The code I attempted to use to restart was probably terribly off:

os.execl(sys.executable, sys.executable, * sys.argv)

any help is appreciated.

import os

PassCount = 0
SetUp = 0

newpath = r"PassEncryptPY"
if not os.path.exists(newpath):
    os.makedirs(newpath)

f = open("PassEncryptPY/PassEncryptPY_PTF.txt", "a")
g = open("PassEncryptPY/PassEncryptPY_ETF.txt", "a")

if os.path.getsize(r"PassEncryptPY/PassEncryptPY_PTF.txt") == 0:
    print("we have detected that you dont have a password")
    pas = input("Your password: ")
    f.write(pas)
    SetUp = SetUp + 1

if os.path.getsize(r"PassEncryptPY/PassEncryptPY_ETF.txt") == 0:
    print("we have detected that you dont have a email")
    ema = input("Your email: ")
    g.write(ema)
    SetUp = SetUp + 1

if SetUp != 0:
    print("Set Up complete, restarting")
    f.flush()
    g.flush()
    os.fsync(f.fileno())
    os.fsync(g.fileno())
    import sys
    os.execl(sys.executable, sys.executable, * sys.argv)

f.close()
g.close()
6
  • you are missing a ton of relevant details ... but that command looks reasonable ... we have no idea what went wrong you didnt even give us a traceback or how you launched the program or anything Commented Mar 14, 2018 at 23:24
  • It restarts the python shell but not the code itself. Commented Mar 14, 2018 at 23:27
  • I stripped this down to a quick test, and it seems to do exactly what you want, as you'd expect—it prints 'Hi!' every second until you kill it. Commented Mar 14, 2018 at 23:52
  • However, depending on your platform and Python version, if the script isn't in the current working directory, sys.argv[0] might not be enough to run it. Any chance that's your problem? If you don't know, try printing out __FILE__, sys.argv[0], and os.getcwd() right before the execl. Commented Mar 14, 2018 at 23:54
  • Also, what do you mean "It restarts the python shell but not the code itself"? What exactly happens that makes you think Python restarted but your code didn't? Commented Mar 14, 2018 at 23:59

1 Answer 1

1

sys.executable is not what you expect probably. (assuming you run the script with ./some_name.py rather than python ./some_name.py) It will almost always be the interpreter rather than the script you're running.

You probably wanted something like:

own_name = sys.argv[0]
os.execl(own_name, own_name, *sys.argv[1:])

Print your sys.executable and sys.argv to see the difference.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm pretty sure sys.executable is exactly what he's expecting it to be. His code will exec, e.g., python myscript.py myarg myarg, which is exactly what it should be doing.
@abarnert added an explanation about which way the script it called - I think it addresses what you're thinking of
But even if the script is called with ./some_name.py, calling it with python some_name.py (as long as you use the right Python in the shbang line—which you will, because that's what will be in sys.executable), it will still do the right thing. His version works for both ./some_name.py and python some_name,py (and also pypy3.5 some_name.py and so on); yours only works for the first one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.