0

I'm trying to find a better way to have os.walk prioritize one type of subdirectory over the other, without running the entire loop twice.

Here's an example: Say my directory is as follows-

Root

Root/1

Root/2

Root/3

Root/1/second_task

Root/2/first_task

Root/2/second_task

Root/3/first_task

Considering that first/second_task are folders with files inside, and that for every such folder the program runs a certain follow-up code.

I want to be able to make the os.walk loop search for the first_task folders and perform the actions needed first, before doing so for any second_task folders.

For now I just run the os.walk twice, first searching for first_task, and then in the second time for any other tasks, but I've figured there must be a better way.

Thanks!

  • Have you tried `glob.glob("/**/first_task/*")` [glob.glob](https://docs.python.org/3/library/glob.html), or listdir? it should be [faster](https://stackoverflow.com/a/27565420/20599896) then os.walk (still needs two calls) – Cpt.Hook Dec 12 '22 at 07:48

0 Answers0