Yield in a recursive function

后端 未结 9 526
独厮守ぢ
独厮守ぢ 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:15

    That calls explore like a function. What you should do is iterate it like a generator:

    if stat.S_ISDIR(stat_info.st_mode):
      for p in explore(path):
        yield p
    else:
      yield path
    

    EDIT: Instead of the stat module, you could use os.path.isdir(path).

提交回复
热议问题