-2

I've been looking around the web and I found no articles saying how to close a terminal using Python code.

This is my code:

if optionInput == "6":
exit()

which quits the script, but does not close the terminal the script was running within.

I want it to close the terminal in which the script runs instead of just ending the terminal process which is running the Python script.

Is there any way to do that?

Claudio
  • 7,474
  • 3
  • 18
  • 48
xKwm1
  • 19
  • 1
  • Why do you need to close the terminal? – Ismail Hafeez Apr 13 '21 at 02:43
  • This is going to depend on, at minimum, the OS, the terminal emulator, and probably the shell running in the terminal. – Silvio Mayolo Apr 13 '21 at 02:45
  • 1
    If Python didn't start the terminal, Python should not close the terminal. What if the session contains state from before the Python program ran which the user wants to keep? – tripleee Apr 13 '21 at 03:02
  • This question is not as suggested by closing reason a duplicate. It asks for closing the terminal, not for exiting the script. – Claudio Feb 23 '23 at 13:42
  • Hey look, another example of people not answering the question that was asked, and instead telling OP they are wrong for even trying to do such a thing. – Kurt E. Clothier Jun 21 '23 at 14:27

1 Answers1

3

try this

import os, signal
os.kill(os.getppid(), signal.SIGHUP)
senpai
  • 134
  • 1
  • 7