How to check/change a general file's status as being read?

159 views Asked by At

There is a folder which contains many files in it. The users of my application will work in this folder simultaneously. This application uses Path.rglob() to loop over the files in the folder. I would like to develop a mechanism that when a file is being processed/opened by a user, this file will be automatically skipped by other users.
My questions are the following:

  1. Can I check the status of a file being read/opened?
  2. Is there a scheme which is able to solve this issue in general? Eg, using external lock file to indicate a file is being read or not.
1

There are 1 answers

2
Tamir On

You can use something based on:

import psutil

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

to generate a list of all open files. You can then use the generated list to decide at runtime whether to open a file or not.