-1

Is there a command to close a python program?
if I run with os.system("name.py"), how can I stop it from running?

os.system("name.py")
CodeCop
  • 1
  • 2
  • 15
  • 37
  • 1
    https://stackoverflow.com/questions/6549669/how-to-kill-process-and-child-processes-from-python – JacobIRR Apr 03 '18 at 23:48
  • but where do I write the name? – CodeCop Apr 03 '18 at 23:50
  • The easy answer is: don't run it with `os.system`. The docs for [`os.system`](https://docs.python.org/3/library/os.html#os.system) tell you that pretty clearly: "The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. See the Replacing Older Functions with the subprocess Module section in the subprocess documentation for some helpful recipes." If you use `subprocess`, you can spawn a child and then kill it later. – abarnert Apr 03 '18 at 23:50
  • 1
    This is at least the third question you've posted in the past few weeks where the answer is "If you just use `subprocess` instead of `os.system` that process wouldn't even come up in the first place." Why are you so resistant to doing things the easy way? Are you even looking at the answers you get for these questions? (You haven't accepted any of them.) – abarnert Apr 03 '18 at 23:52
  • Sorry, I am stuck at somewhere and I can't change the whole code just to use subprocess, I didn't accept because there was this red message popping each time I tried to accept. Why are you angry? sorry... – CodeCop Apr 03 '18 at 23:55

1 Answers1

2

Use subprocesses instead.

import subprocess

# Start
proc = subprocess.Popen(["name.py"])

# Stop
proc.terminate()
Nolan
  • 175
  • 9