'List all files currently open in a program with Python/psutil

I'm trying to access all files that are currently open in a program (for example Atom, MS Word, notepad++ etc.). Ideally, this code works for several programs. I can successfully print all open files (so it seems) but the code doesn't return open documents, e.g. opened *.docx files in Word. Perhaps psutil is the wrong approach? This is my working code:

import psutil

def running(pname):
    pname = pname.lower()
    for p in psutil.process_iter(attrs=['name', 'pid']):
        if pname in p.info['name'].lower():
            print(f'{pname} is running with {pid}')
            
            ppid = psutil.Process(pid)
            list_files = ppid.open_files()
            print(list_files)
            
    print(f'{pname} is not running')

running('word')

Can I extend this code to get opened files?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source