1

I need to print a list of all files in sub-directories of the directory "H:\Reference_Archive\1EastRefsJan2014". I am currently using the code:

for root, dirs, files in os.walk("H:\Reference_Archive\1EastRefsJan2014"):
    for name in files:
        print os.path.join(root, name)

The code works and I get a long list of files if I run it only on the root directory ("H:\Reference_Archive"), but when I try to run it on the sub-directory as it is written above, nothing is returned or printed. The path that is written above contains several more sub-directories which all contain files. I have double checked that I have the pathway correct.

mmoore
  • 174
  • 1
  • 9
  • Your assumption about what `os.walk` returns is wrong. Are you sure the code you posted runs? – Mad Physicist Aug 08 '16 at 17:54
  • 1
    Well, to start, each iteration of `os.walk` returns a triple: `root, dirs, files` – juanpa.arrivillaga Aug 08 '16 at 17:54
  • I am getting the same result when I include dirs. It gives me a list of all files in all sub-directories if I only include the root path, but gives me nothing if I include the sub-directory that I would like to view. – mmoore Aug 08 '16 at 18:20

2 Answers2

0

try this, you omitted dirs

for root, dirs, files in os.walk("H:\Reference_Archive\1EastRefsJan2014"):
    for name in files:
        print os.path.join(root, name)
Dulmandakh
  • 21
  • 2
  • I am getting the same result when I include dirs. It gives me a list of all files in all sub-directories if I only include the root path, but gives me nothing if I include the sub-directory that I would like to access. – mmoore Aug 08 '16 at 18:19
0

Finally figured out that the os.walk function was not working with my folder because the folder name started with a number. Once I changed the name of the folder, it worked properly.

mmoore
  • 174
  • 1
  • 9