Roger M

There are a lot of use case that multiple thread operate same file, and one thread cannot process the file until the prev thread ended and release the file. It’s confusing how check file occupation in linux & window, so I did some digging.
1.linux:iterate PIDs in /proc.

    dir = '/proc/'+str(pid)+'/fd'
    if not os.access(dir,os.R_OK|os.X_OK): return

    for fds in os.listdir(dir):
        for fd in fds:
            full_name = os.path.join(dir, fd)
            try:
                file = os.readlink(full_name)
                if file == '/dev/null' or \
                  re.match(r'pipe:\[\d+\]',file) or \
                  re.match(r'socket:\[\d+\]',file):
                    file = None
            except OSError as err:
                if err.errno == 2:     
                    file = None
                else:
                    raise(err)

            yield (fd,file)```
2.Windows :you can use is_used(),like inside this answer.

#python

In Python, How to check if a file is occupied or not ?
1.40 GEEK