Search code examples
pythonwindowspsutil

getting svchost path by PID using python 2.7


i am trying to get svchost.exe path by PID using python. i tried it using psutil but i got access denied error.

here is my code-

  import psutil
  p = psutil.Process(1832)
  print p.exe()

Solution

  • you can get process path using wmi module.

    Here is a sample code for you. This code fids all process named svchost and return path to the process. This returns full information on process.

    import wmi
    import psutil
    
    c = wmi.WMI ()
    process = psutil.Process(2276)
    
    process_name = process.name()
    
    for process in c.Win32_Process(name=process_name):
      if process.ExecutablePath:
                print (process.ExecutablePath)
    

    Output

     c:\windows\system32\svchost.exe
    c:\windows\system32\svchost.exe
    c:\windows\system32\svchost.exe