Yield in a recursive function

后端 未结 9 504
独厮守ぢ
独厮守ぢ 2021-01-30 10:49

I am trying to do something to all the files under a given path. I don\'t want to collect all the file names beforehand then do something with them, so I tried this:

<         


        
9条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-30 11:16

    Change this:

    explore(path)
    

    To this:

    for subpath in explore(path):
        yield subpath
    

    Or use os.walk, as phooji suggested (which is the better option).

提交回复
热议问题