The reason is that subprocess.Popen
creates a new process. So, what exactly is happening in your code is that you are creating a new process and then you are closing that new process. Instead, you need to find out the process id and kill it.
Note: The shell command work on the Windows system. To use them in a UNIX environment, you need to change the shell commands
import os
import subprocess
pid = subprocess.getoutput('tasklist | grep Notepad.exe').split()[1]
# we are taking [1] because this is the output produced by
# 'tasklist | grep Notepad.exe'
# Image Name PID Session Name Session Mem Usage
# ========== ==== ============= ======= =========
# Notepad.exe 10936 Console 17 16,584 K
os.system(f'taskkill /pid {pid}')
EDIT: To kill a specific process use the code below
import os
import subprocess
FILE_NAME = 'test.pdf' # Change this to your pdf file and it should work
proc = subprocess.getoutput('tasklist /fi "imagename eq Acrobat.exe" /fo csv /v /nh')
proc_list = proc.replace('"', '').split('\n')
for x in proc_list:
p = x.split(',')
if p[9].startswith(FILE_NAME):
pid = p[1]
os.system(f'taskkill /pid {pid}')