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:
import os
import stat
def explore(p):
s = ''
list = os.listdir(p)
for a in list:
path = p + '/' + a
stat_info = os.lstat(path )
if stat.S_ISDIR(stat_info.st_mode):
explore(path)
else:
yield path
if __name__ == "__main__":
for x in explore('.'):
print '-->', x
But this code skips over directories when it hits them, instead of yielding their contents. What am I doing wrong?
The problem is this line of code:
What does it do?
explore
with the newpath
explore
runs, creating a generatorexplore(path)
was executed . . .Why is it discarded? It wasn't assigned to anything, it wasn't iterated over -- it was completely ignored.
If you want to do something with the results, well, you have to do something with them! ;)
The easiest way to fix your code is:
When you are confident you understand what's going on, you'll probably want to use
os.walk()
instead.Once you have migrated to Python 3.3 (assuming all works out as planned) you will be able to use the new
yield from
syntax and the easiest way to fix your code at that point will be:Try this:
Change this:
To this:
Or use
os.walk
, as phooji suggested (which is the better option).