我正在尝试关闭程序,在这种情况下spotify,但我不断得到语义错误。这是我的代码:
sup_programs.txt
{
'spotify': ['C:/Users/Daniiar/AppData/Roaming/Spotify/Spotify.exe']
}
脚本:
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')
我使用了kill()和terminate()它们都没有工作和os.system(' TASKKILL') 是否还有其他方法或使用现有方法?
顺便说一句,我使用的是Windows 10和python 3.5。谢谢你的帮助答案 0 :(得分:4)
你的open_program
函数是递归的???
无论如何,要打开程序,可以通过路径名来实现。然后它返回子进程的句柄。 要关闭它,只需在该句柄上调用terminate方法
下面的例子打开记事本,等待3秒,然后关闭它。
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)