python - Get full path of currently open files -
i'm trying code simple application must read open files within directory. more specificly, want list of files open anywhere inside documents folder, don't want processes' ids or process name, want full path of open file.
the thing haven't quite found that. couldn't neither in linux shell (using ps , lsof commands) nor using python's psutil library. none of these giving me information need, path of open files in dir.
any advice?
p.s: i'm tagging python question (besides os related tags) because plus if done using python library.
this seems work (on linux):
import subprocess import shlex cmd = shlex.split('lsof -f n +d .') try: output = subprocess.check_output(cmd).splitlines() except subprocess.calledprocesserror err: output = err.output.splitlines() output = [line[3:] line in output if line.startswith('n./')] # out[3]: ['file.tmp']
it reads open files current directory, non-recursively.
for recursive search, use +d
option. keep in mind, vulnerable race condition - when ouput, situation might have changed already. best try (open file), , check failure, e.g. open file , catch exception or check null file
value in c.
Comments
Post a Comment