Skip to content Skip to sidebar Skip to footer

Stop Running Python Script Without Killing The Interpreter

Before, I were able to kill a python script started with execfile('somescript.py') while in interpreter by pressing Ctrl + C without killing the interpreter. This would cause a Key

Solution 1:

Usually, this is done with a try statement:

>>> def f():
...     try:
...         exec(open("somefile.py").read())
...     except Exception as e: print(e)
... 
>>> f()
^CTraceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in f
  File "<string>", line 4, in <module>
  File "<string>", line 3, in g
KeyboardInterrupt
>>>

somefile.py:

def g():
    while True: pass
g()

Post a Comment for "Stop Running Python Script Without Killing The Interpreter"