3

I'm trying to use os.walk in order to access all files and all subfolders within a given folder. At first, my code looks like this:

for root, dirs, files in os.walk("/home/DataScience"):   
    for dirName in dirs:
          print dirName

However, the output of the above puts the files variable into a list-- I need it to iterate over each object within the list instead.

So I go ahead and put in an additional for loop as such:

for root, dirs, files in os.walk("/home/DataScience"):   
    for dirName in dirs:
        for fileName in files:
            print dirName

Now, however, there is no output. For some reason, it seems as if putting the additional loop makes it such that the print statement on the 4th line doesn't output properly.

My question then is this: why does this additional for loop eliminate my output?

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Z R
  • 121
  • 1
  • 7

2 Answers2

4

You need two separated loop for directories and files:

for parent, dirs, files in os.walk("/home/DataScience"):   
    for dirname in dirs:
        print dirname
    for filename in files:
        print filename
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • Interesting-- any reason why? – Z R Nov 03 '15 at 04:07
  • @ZR, Because `dirs` is a list of directories, `files` is a list of files. – falsetru Nov 03 '15 at 04:08
  • The way in which I'm structuring the rest of the script seems to require me to iterate over the files after having iterated over one part of the dirs, hence why I was so keen on embedding one within the other. – Z R Nov 03 '15 at 04:08
  • @ZR: if `dirs` is empty, there are no elements within it, and no reason for any inner loops to be executed – inspectorG4dget Nov 03 '15 at 04:08
  • But it seems as if that's never the case. I don't think there are any situations in which dirs is empty. – Z R Nov 03 '15 at 04:11
  • @ZR, `os.walk` recurses for you. and for each iteration, it yields subdirectories and files in a directory. – falsetru Nov 03 '15 at 04:14
0

If you only care about the files, not the folders in a directory, and you want to work with them (say, to open them), this is what you want to do:

import os
for root, subdirs, files in os.walk(directory):
    for file in files:
        print(root + os.sep + file)
        # or whatever file operation you're interested in

subdirs, or what you're calling dirs, is just a list of the folders in root. On each step, os.walk() will step into the next subdirectory inside of root, making THAT root. So, you don't need to go through the folders yourself.

BlivetWidget
  • 10,543
  • 1
  • 14
  • 23
  • 1
    You could use `os.path.join(root, file)`. – falsetru Nov 03 '15 at 04:37
  • @falsetru That works just as well. Do you know if either option has any inherent advantage? They are both readable and have the same dependencies. – BlivetWidget Nov 03 '15 at 05:06
  • According to [the documentation](https://docs.python.org/2/library/os.html#os.sep): ... Note that knowing this is not sufficient to be able to parse or concatenate pathnames — use os.path.split() and os.path.join() ... – falsetru Nov 03 '15 at 05:24
  • @falsetru interesting, I wonder why, since they don't explain a reason. os.sep hasn't ever caused me any trouble, though admittedly I'm not working on cross-platform applications. – BlivetWidget Nov 03 '15 at 15:50