check what files are open in Python

To list all open files in a cross-platform manner, I would recommend psutil.

#!/usr/bin/env python
import psutil

for proc in psutil.process_iter():
    print(proc.open_files())

The original question implicitly restricts the operation to the currently running process, which can be accessed through psutil’s Process class.

proc = psutil.Process()
print(proc.open_files())

Lastly, you’ll want to run the code using an account with the appropriate permissions to access this information or you may see AccessDenied errors.

Leave a Comment