I have a script in which I use subprocess.Popen to start an external program and process.kill() to kill it pretty much as soon as it's started. I've been getting Windows Error [5] (Access Denied) every time the script tries to kill it. I've realized that the pid of the program is actually changing after it's opened. Is there a way, in Python, to monitor the process for the change, or to just retrieve the new pid?
Here is the code:
import subprocess
import time
proc = subprocess.Popen(Path/to/WinSCP.exe)
time.sleep(2)
proc.kill()
The error:
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
return self.func(*args)
File "C:\Path", line 231, in __call__
self.scpsetup()
File "C:\Path", line 329, in scpsetup
proc.kill()
File "C:\Python27\lib\subprocess.py", line 1019, in terminate
_subprocess.TerminateProcess(self._handle, 1)
WindowsError: [Error 5] Access is denied