Is it possible to get file path (not application path) from process ID using python?

51 views Asked by At

I have a file saved to desktop. I can get the process ID of the file using the python code below. However, in my case, there user might need to select a particular application instance from a list of open instances, so I need to display the file path of all the instances of my app. Is there a way I can obtain the file path of the application using python?

I used psutil to get PID corresponding to a given application. Here the notepad file is saved to C:/Users/username/Desktop/. The code is given below

import psutil
for proc in psutil.process_iter():
    if proc.name() == "Notepad.exe":
        print(f"Process ID: {proc.pid} => Application: {proc.name()} => Path: {proc.exe()}")

This gives me an output of

Process ID: 20188 => Application: Notepad.exe => Path: C:\Program Files\WindowsApps\Microsoft.WindowsNotepad_11.2311.35.0_x64__8wekyb3d8bbwe\Notepad\Notepad.exe

I also tried sys.argv, but it doesn't output the result I am looking for.

Is there a way to get file location instead of application path using python? Thank you.

0

There are 0 answers