2

I am trying to close the program, in this case spotify, but I am keep getting semantic mistake. Here is my code:

sup_programs.txt

{ 
'spotify': ['C:/Users/Daniiar/AppData/Roaming/Spotify/Spotify.exe']
}

script:

class Path(object):
    import ast
    sup_programs_txt = open('sup_programs.txt', 'r')
    s_p = sup_programs_txt.read()
    paths = ast.literal_eval(s_p)
    sup_programs_txt.close()
paths = Path().paths
program_name = 'spotify'


def open_program(path_name):
    import subprocess
    subprocess.Popen(path_name)
    open_program(paths[program_name])


def close_program(path_name):
    import subprocess
    p = subprocess.Popen(path_name)
    p.terminate()


yes = input(" ... ")
if 'close' in yes or 'yes' in yes:
    close_program(paths[program_name])
else:
    print('too bad')

I used kill() and terminate() neither of them have worked and os.system('TASKKILL ') Is there any other methods or am using existing ones incorrectly?

BTW, I am using windows 10 and python 3.5 . Thank you for your help

2
  • Why would starting a new process and killing it, kill a different already running process? Commented Jun 26, 2016 at 5:07
  • you have to store the "p" of the open process and use p.terminate() on this p, not another instance of that executable. Commented Jun 26, 2016 at 5:40

1 Answer 1

5

your open_program function is recursive ???

Anyway, to open your program, you can do it by the path name. It then returns a handle on a subprocess. To close it, just call the terminate method on that handle

example below opens notepad, waits 3 seconds, then closes it.

import time
import subprocess

def open_program(path_name):

    return subprocess.Popen(path_name)

def close_program(p):
    p.terminate()

p=open_program("notepad")
time.sleep(3)
close_program(p)
Sign up to request clarification or add additional context in comments.

Comments

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.