0
from os.path import join

string=" Congratulations, you are about to embark upon one of life’s "

path=r"C:\Users\Nord.Kind\Desktop"
file="test.txt"

quit() 

# This should not execute, but it does!!
with open(join(path,file),"w")as wfile:
    wfile.write(string)

wfile.close()

In the above code example the code still executes the write in file command, even though its after a quit.

The same behavious occures when I use exit.

I am using Spyder 3.6

Also the kernel restarts each time I use exit or quit.

Any help?

Nord.Kind
  • 81
  • 2
  • 10
  • There's no way that code writes anything to a file, because it should throw a `NameError: name 'read' is not defined`. I don't know what code you're executing, but it's not the code you posted. – Aran-Fey Aug 11 '18 at 08:38
  • In spyder, there may be a residual `read` variable already defined from previous runs @Aran-Fey - The OP should restart the kernel and run the program with a clean kernel. – Reblochon Masque Aug 11 '18 at 08:39
  • @ReblochonMasque The OP said the kernel restarts every time though. Something doesn't add up. – Aran-Fey Aug 11 '18 at 08:41
  • yes, I saw that after I commented... something does not add up. – Reblochon Masque Aug 11 '18 at 08:42
  • I fixed the variable in my Post. Ofc it was named wrong. The issue stands: using "quit()" and "exit()" wont stop the code from writing into the file! Thats a behaviour I would not expect! – Nord.Kind Aug 11 '18 at 09:50

3 Answers3

2

One way is to use sys.exit() in lieu of quit()

import sys

...   # code that executes

sys.exit()

...   # this code won't execute

However, as noted by @AranFey in the comments, your code will throw an error if it attempts to execute the last part where the variable read is not defined.

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • Sys.exit does indeed work. However it stands the question why quit() and exit() both lead to a "reconnect to Kernel" and to the code writing stuff into the file. – Nord.Kind Aug 11 '18 at 09:51
  • Maybe a spyder glitch? Have you checked their mailing list, or their issues? – Reblochon Masque Aug 11 '18 at 09:54
  • If you feel that my answer helped you, you could consider [what to do when someone answers my question](https://stackoverflow.com/help/someone-answers), and [how to accept my answer](http://meta.stackexchange.com/a/5235) – Reblochon Masque Aug 12 '18 at 15:47
2

(Spyder maintainer here) Your question contains this comment:

Also the kernel restarts each time I use exit or quit.

That's the behavior of the IPython kernel we use as a backend to execute users code. Those commands kill the kernel and that forces a kernel restart to maintain its associated console active. I'm afraid there's nothing you can do about it.

Note: The same happens in the Jupyter notebook.

Carlos Cordoba
  • 33,273
  • 10
  • 95
  • 124
1

You can use SystemExit:

# Code that will run
raise SystemExit
# Code that will not run

sys.exit() also raises this error but this doesn't require importing sys.

haklir
  • 76
  • 4
  • "raise" does indeed work. However it stands the question why quit() and exit() both lead to a "reconnect to Kernel" and to the code writing stuff into the file. – Nord.Kind Aug 11 '18 at 09:51