Is there a way to efficiently yield every file in a directory containing millions of files?

前端 未结 6 1664
萌比男神i
萌比男神i 2020-12-01 18:53

I\'m aware of os.listdir, but as far as I can gather, that gets all the filenames in a directory into memory, and then returns the list. What I want, is a way t

6条回答
  •  失恋的感觉
    2020-12-01 19:00

    I think what you are asking is impossible due to the nature of file IO. Once python has retrieved the listing of a directory it cannot maintain a view of the actual directory on disk, nor is there any way for python to insist that the OS inform it of any modifications to the directory.

    All python can do is ask for periodic listings and diff the results to see if there have been any changes.

    The best you can do is create a semaphore file in the directory which lets other processes know that your python process desires that no other process modify the directory. Of course they will only observe the semaphore if you have explicitly programmed them to.

提交回复
热议问题