0

I try to close a pdf which I opened with the following process:

import subprocess
openpdffile = subprocess.Popen([file_path], shell=True)    

I tried

openpdffile.kill()

But that keeps the pdf open in my pdf reader. Any suggestions?

Many thanks.

Rolf12
  • 701
  • 1
  • 6
  • 20
  • 2
    Does this answer your question? [How to terminate a python subprocess launched with shell=True](https://stackoverflow.com/questions/4789837/how-to-terminate-a-python-subprocess-launched-with-shell-true) – woockashek Oct 19 '21 at 13:20
  • Many thanks. I tried a few of the solutions proposed there (such as the process group but the os.killpg seems to work only in Linux – Rolf12 Oct 19 '21 at 14:04

2 Answers2

0

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}')
Ayush Gupta
  • 1,166
  • 2
  • 9
  • 25
  • Many thanks Ayush for your help! But how can I dod that with the pdf? Would be also possible to justclose that specific pdf? – Rolf12 Oct 19 '21 at 14:02
  • @Rolf12 It's a little difficult because the process list usually contains the application name, say if you open the pdf in Adobe then the process list will have `adobe.exe` but it won't show which pdf file you have opened. So, it's not easy to close that specific pdf however, you can close all pdf files by getting the list of `pids`. – Ayush Gupta Oct 19 '21 at 14:08
  • Many thanks. I tried this but in the variable I get the string "is"? so get the Error that the process is not found. I replaced notepad.exe with Acrobat.exe – Rolf12 Oct 19 '21 at 14:19
  • @Rolf12 After a lot of research, I found out that you can in fact get the window title and then check the filename in the title, get the process id and kill it. I am posting the solution in am edit. Just give me a few minutes – Ayush Gupta Oct 19 '21 at 14:36
  • Many thanks Ayush! I tried it. It looks like the first run worked but after that I always get "Index out of Range" errors as proc says "No tasks are running which match the specified criteria" But Acrobat.exe is def running and the excel is opn – Rolf12 Oct 19 '21 at 16:37
  • @Rolf12 What do you mean `the excel is opn`? – Ayush Gupta Oct 19 '21 at 20:17
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/238355/discussion-between-rolf12-and-ayush-gupta). – Rolf12 Oct 20 '21 at 11:19
  • Sorry I meant acrobat.exe is still running. – Rolf12 Oct 21 '21 at 06:26
  • @Rolf12 Are you sure that it is the same file? – Ayush Gupta Oct 21 '21 at 10:21
  • I added a time sleep then it worked, seems like it took Adobe too much time to open the file. – Rolf12 Oct 27 '21 at 07:41
0

You can get pid when done subprocess things, and deterimine which to kill for your convenience.

here you can learn how to get pid of a subprocess: https://stackoverflow.com/a/7989942/13837927

tomy0608
  • 317
  • 1
  • 9