0

Referred: SO Q1 and SO Q2

What I have tried? In the former link given in the question and I have tried executing it and I get Process Name,Process Locations and process ID. And I referred the internet and modified the script given in the popular answer of SO Q1 and modified like this

class GetProcess:
def __init__(self):
    cmd = 'WMIC PROCESS get Commandline'
    proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    for line in proc.stdout:
        print(line)

when I am executing the script I get locations with weird data like this

b'"C:\Program Files\WindowsApps\Microsoft.Windows.Photos_17.214.10010.0_x64__8wekyb3d8bbwe\Microsoft.Photos.exe" -ServerName:App.AppXzst44mncqdg84v7sv6p7yznqwssy6f7f.mca

Is there any way to get the location alone instead of getting what is running what?

I can see that this can be done using with some third party libraries but I need to know whether there is any solution which I can get the process without any third party library

Community
  • 1
  • 1

1 Answers1

0

You want to take the command lines such as this:

b'"C:\\Program Files\\WindowsApps\\Microsoft.Windows.Photos_17.214.10010.0_x64__8wekyb3d8bbwe\\Microsoft.Photos.exe" -ServerName:App.AppXzst44mncqdg84v7sv6p7yznqwssy6f7f.mca

And extra only the executable path, which is the first part. It seems like you just need to parse it by looking for the first (possibly quoted) string, which you can do using re, the regular expression module in Python.

The b'...' wrapper you see around the string indicates it is of the bytes type in Python 3, as opposed to Unicode. That's normal for the output of Popen().

By the way, subprocess.check_output() is probably better suited and safer for your task.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436