Last active
January 24, 2024 02:09
-
-
Save lebedov/b0144d07d3bec93c5d14 to your computer and use it in GitHub Desktop.
Python functions for finding open files and PIDs that have opened a file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Python functions for finding open files and PIDs that have opened a file. | |
""" | |
import numbers | |
import subprocess | |
try: | |
from subprocess import DEVNULL | |
except ImportError: | |
import os | |
DEVNULL = open(os.devnull, 'wb') | |
def get_open_files(*pids): | |
""" | |
Find files opened by specified process ID(s). | |
Parameters | |
---------- | |
pids : list of int | |
Process IDs. | |
Returns | |
------- | |
files : list of str | |
Open file names. | |
""" | |
for pid in pids: | |
if not isinstance(pid, numbers.Integral): | |
raise ValueError('invalid PID') | |
files = set() | |
for pid in pids: | |
try: | |
out = subprocess.check_output(['lsof', '-wXFn', '+p', str(pid)], | |
stderr=DEVNULL) | |
except: | |
pass | |
else: | |
lines = out.strip().split('\n') | |
for line in lines: | |
# Skip sockets, pipes, etc.: | |
if line.startswith('n') and line[1] == '/': | |
files.add(line[1:]) | |
return list(files) | |
def get_pids_open(*files): | |
""" | |
Find processes with open handles for the specified file(s). | |
Parameters | |
---------- | |
files : list of str | |
File paths. | |
Returns | |
------- | |
pids : list of int | |
Process IDs with open handles to the specified files. | |
""" | |
for f in files: | |
if not isinstance(f, basestring): | |
raise ValueError('invalid file name %s' % f) | |
pids = set() | |
try: | |
out = subprocess.check_output(['lsof', '+wt']+list(files), | |
stderr=DEVNULL) | |
except Exception as e: | |
out = str(e.output) | |
if not out.strip(): | |
return [] | |
lines = out.strip().split('\n') | |
for line in lines: | |
pids.add(int(line)) | |
return list(pids) |
That was helpful!
Can you also add OS independent version of function 'get_pids_open'?
@Aman0509 See https://psutil.readthedocs.io for a cross-platform approach.
As for a cross platform solution, this is what I used:
import psutil
file = "/home/username/.bashrc" # some file
for pid in psutil.process_iter():
try:
for opened_file in pid.open_files():
if file == opened_file.path:
# do something here
# and stop
break
except psutil.AccessDenied:
continue
This uses the psutil module
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
python3-fix for get_pids_open