1

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.
Lion Lai
  • 1,862
  • 2
  • 20
  • 41
  • I gather you have no control over HOW they are being read, just that they are being read? i.e. Are they writing programs to open the file, or could they be just, e.g. editing a file? If they are writing programs, you could "serve" the file name they should open and keep track within that server. – RufusVS Apr 11 '23 at 18:40

1 Answers1

0

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.

Tamir
  • 1,224
  • 1
  • 5
  • 18
  • This will show all opened files on my pc. Is there a more elegant way to do it? – Lion Lai Nov 24 '21 at 13:48
  • I know, unfortunately I am not aware of a different method that isn't in the OS level (i.e OS models as part of the kernel) – Tamir Nov 24 '21 at 14:09