1

I need the pid(process id) to kill the task afterwards. It's a OneNote file shortcut on my PC.

If I start with it shell=True, the pid from that isnt the same as the one in Task Manager, so this wont work:

os.chdir("E:\Anyagok\Programozás\Python\projekts\Én projektjeim")
am=subprocess.Popen("cold_turkey.url",shell=True)
os.system("TASKKILL /F /PID {}".format(am.pid)) 

So, first try:

os.chdir("E:\Anyagok\Programozás\Python\projekts\Én projektjeim")
subprocess.Popen("E:\Anyagok\Programozás\Python\projekts\Én projektjeim\cold_turkey.url")

Traceback (most recent call last):

  File "<ipython-input-236-87374b9d7654>", line 1, in <module>
    subprocess.Popen("E:\Anyagok\Programozás\Python\projekts\Én projektjeim\cold_turkey.url")

  File "E:\Download\PROGIK\ANACONDA\lib\site-packages\spyder\utils\site\sitecustomize.py", line 210, in __init__
    super(SubprocessPopen, self).__init__(*args, **kwargs)

  File "E:\Download\PROGIK\ANACONDA\lib\subprocess.py", line 709, in __init__
    restore_signals, start_new_session)

  File "E:\Download\PROGIK\ANACONDA\lib\subprocess.py", line 997, in _execute_child
    startupinfo)
I have to open it without shell to get the proper pid, but I just cant open it.

1. method, just the link

    subprocess.Popen("E:\Anyagok\Programozás\Python\projekts\Én projektjeim\cold_turkey.url")
    WindowsError: [Error 193] %1 is not a valid Win32 application
  1. method:

    subprocess.Popen("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Office 2016\OneNote 2016.lnk", "E:\Anyagok\Programozás\Python\projekts\Én projektjeim\cold_turkey.url") Traceback (most recent call last):

      File "<ipython-input-238-80cb9d5ed425>", line 2, in <module>
        "E:\Anyagok\Programozás\Python\projekts\Én projektjeim\cold_turkey.url")
    
      File "E:\Download\PROGIK\ANACONDA\lib\site-packages\spyder\utils\site\sitecustomize.py", line 210, in __init__
        super(SubprocessPopen, self).__init__(*args, **kwargs)
    
      File "E:\Download\PROGIK\ANACONDA\lib\subprocess.py", line 609, in __init__
        raise TypeError("bufsize must be an integer")
    
    TypeError: bufsize must be an integer
    

How can it be opened with Popen() so that I can have the pid?

There has to be a better way than actually using This

Chris
  • 951
  • 10
  • 26
  • As a side note, [do not unescaped backslashes for Windows paths in non-raw string literals](https://stackoverflow.com/questions/2953834/windows-path-in-python). Your code only happens to get as far as it does because you're lucky that all of your backslash escapes like `\p` happen to be meaningless. – abarnert Mar 26 '18 at 20:18
  • Anyway, `.url` files are not a kind of program that Windows knows how to open. The _shell_ knows how to open them. but that of course means you need `shell=True`. Also, the way the shell opens them is by asking your existing browser process to open the URL, so there is no new process to give you a PID for unless no browser was running, and you wouldn't want to `kill` it even if there way. (Sure, most browsers do multi-process isolation nowadays, but that doesn't mean you can kill the top-level process without the whole browser going away.) – abarnert Mar 26 '18 at 20:20
  • Also, `Popen` takes a list of arguments, where the first one is your program. If you have no arguments to pass to the program, call it with a single-element list. If you do have arguments, call it with a two-element list, not two separate arguments. – abarnert Mar 26 '18 at 20:21
  • And finally, `.lnk` files are just shortcuts used by Windows Explorer to point to a real file. Windows itself doesn't understand them, just Explorer. You could ask Explorer to open the .lnk to open the .url, but that's no better than using the shell, and of course you'd get the PID of a new Explorer process. But in this case, I believe there are functions you can access via the `winapi` package from `PyWin32` to find the target for a `.lnk` file, and then you can `Popen` that. – abarnert Mar 26 '18 at 20:23
  • It *may* be possible to get a PID when running a shell shortcut via `ShellExecuteEx`, if a new process is started. This isn't necessarily "Explorer". The details of the shell API are split across several DLLs (e.g. shell32.dll, windows.storage.dll, propsys.dll), and most functionality should be available even if Explorer isn't running as the primary shell. – Eryk Sun Mar 26 '18 at 20:34
  • If you install PyWin32, you can use `import win32con;` `from win32com.shell import shell, shellcon;` `result = shell.ShellExecuteEx(lpFile=shortcut_path, fMask=shellcon.SEE_MASK_NOCLOSEPROCESS, nShow=win32con.SW_SHOW)`. The process handle, if returned, will be `result['hProcess']`. – Eryk Sun Mar 26 '18 at 20:37

0 Answers0