2
votes

I'm using os.walk() to check a directory for redundant files and list them out. The pseudo-code looks something like this:

def checkPath(path):
    do the for dirname, dirnames, filenames in os.walk(path) thing here...

pathList = ["path1", "path2"]

for each in pathList:
    checkPath(each)

So this works fine the first run through, I get everything as expected, but on the next os.walk on the second path it just skips right on through...there's nothing in dirname, dirnames, filenames. I did some print statements to check things out, and it's entering the function, but not doing anything for the os.walk() part.

before making the os.walk() part a function to see if it would fix the problem, it was in a for loop inline with the main body. When I tried (just for fun) cleaning up the dirname, dirnames, filenames variables with del, on the second path when the cleanup came it said that the variable dirname did not exist...

So it looks like, whether within a function or not, the successive iterations of os.walk() arent populating...

ideas?

Thanks!


To add some working code as an example, something like this. It doesn't really matter what it's doing, just trying to get the os.walk to walk mult paths:

import os

def checkPath(path):
    for dirname, dirnames, filenames in os.walk(path):
        for filename in filenames:
            print filename

pathList = ["c:\temp\folder1", "c:\temp\folder2"]

for path in pathList:
    checkPath(path)

print "done"

It could be done this way (was trying to see if calling os.walk in a different way, like one of the other commenters suggested, might help), or it can be done inline, whatever works obviously...

thanks again all,

3
What is the path it's skipping? - thegrinner
Show some working code that demonstrates your problem... - Bakuriu
the paths are simple, local folders of test data (spreadsheets, docs, etc.). pathList = ["c:\temp\folder1", "c:\temp\folder2"]. Each of these folders has subfolders with a few files in them. In this case it's processing through the first folder, goes to the second folder, but os.walk seems to yield nothing in the dirname, dirnames, filenames variables... - user1229108
@user1229108 This is prosa, not code. - glglgl
if the path given to os.walk does not exist, the for loop just terminates without assigning anything to the variables or printing anything out. How about adding a print statement above the for loop to make sure the directory exists: print 'checking', path, os.path.isdir(path). - tdelaney

3 Answers

7
votes

Your code works for me, if I use actual paths on my system which refer to non-empty directories.

I suspect you might have an issue with the line...

pathList = ["c:\temp\folder1", "c:\temp\folder2"]

...since both \t and \f are valid escape sequences.

Try...

pathList = ["c:\\temp\\folder1", "c:\\temp\\folder2"]

...and if that's not the problem, then it would help to cite the actual code you're using.

4
votes

os.walk returns a generator :-) http://wiki.python.org/moin/Generators

There are a few workarounds:

use a list

  1. ll = list(os.walk())

  2. call os.walk() each time

  3. use itertools.chain

The code you posted should not have this problem (you call os.walk each time), but it makes me really think about generator exhaustion. So post your code as you wrote it [0]

[0] for example, do you have some sort of predefined argument in your function?

1
votes

Here is a working example

import os

def checkPath(list_path):
    for path in list_path:
        for (path, dirs, files) in os.walk(path):
            print len(files)

checkPath(["F:/","F:/"])

See doc:

Generate the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames).

EDIT:

as mentioned in your answers, os.walk() returns a generator. A generator can be iterated through once only. It is not a structure storing values, but generates the values on the fly, as it is called. That's why your second loop on os.walk(), you have no more results. You may ask os.walk() each time you need it, or store os.walk() to an iterable.