Can I force python3's os.walk to visit directories in alphabetical order? how?

后端 未结 3 490
既然无缘
既然无缘 2020-12-09 15:03

I would like to know if it\'s possible to force os.walk in python3 to visit directories in alphabetical order. For example, here is a directory and some code that will walk

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-09 15:28

    Yes. You sort dirs in the loop.

    def main_work_subdirs(gl):
        for root, dirs, files in os.walk(gl['pwd']):
            dirs.sort()
            if root == gl['pwd']:
                for d2i in dirs:
                    print(d2i)
    

提交回复
热议问题