1

There is a similar question in the .NET version, but I'm asking specifically about the Python os.walk function. I am using the latest Python 3.

Suppose I have a directory, for which there are many files. I am performing an operation that takes awhile on each file, leaving ample time for the root directory to be changed mid loop by another process. Here would be an example, for clarity's sake:

for root, dirs, files in os.walk(rootDirectory):
    for file in files:
        doAReallyLongBlockingCall(file)

Would os.walk detect such changes? If I add files, will they be found by os.walk?

Athena
  • 3,200
  • 3
  • 27
  • 35
  • Try it, using multiprocessing or threading, create files in the tree with one process/thread and walk the tree with the other process/thread. What happened? throw in some randow `time.sleep()`'s. then walk the tree after those two are done, compare the results. – wwii Oct 26 '17 at 18:47

1 Answers1

1

This is not really about os.walk but what the OS does On linux once you open a dir ( there is a syscall for that) the data will not change under your hands

so if in your loop you open a file and take 10 minutes to process it and one of the unprocessed files gets deleted, walk will still give you the name and also any file created after opendir will not be listed

you will need exceptions to handle the cases when you lose a file before you get to it

Sedy Vlk
  • 565
  • 4
  • 12