Hi all,
I have spent hours trying to figure out where I have went wrong with my code for my recursive function to list all the files in a directory, and all of the files in all
of its subdirectories.
This is the code:
It goes down to level 3 of the directory tree, but at one point I get the
error 2, the no such file or directory message. I have checked the
file permissions of the directory that it gets stuck on, and I have no idea why
it is gettintg stuck on that directory and not one of the directories before it.
I would be extremely grateful if someone could lend another set of eyes to my
code and see where I'm going wrong at, and why it goes to level 3 so to speak
of the tree and then fails.
I'm stumped!
Thanks so much in advance. Any and all input would be greatly appreciated!
I have spent hours trying to figure out where I have went wrong with my code for my recursive function to list all the files in a directory, and all of the files in all
of its subdirectories.
This is the code:
Code:
import os
import sys
import dircache
import stat
import string
def dirrecur(currsub):
thispath = os.getcwd()
print thispath
print "\n"
if(len(currsub) == 0): #if len = 0, no subdirectories were found.
os.chdir('..') #go back up one step.
else:
dlist = []
for z in range(0,len(currsub)):
os.chdir(currsub[z])
print currsub[z]
print "\n"
direntries = dircache.opendir('.') #open this directory
for j in range(0,len(direntries)):
statresult = os.stat(direntries[j])
if (stat.S_ISDIR(statresult.st_mode) != 0):
dlist.append(direntries[j]) #dlist is built here.
print direntries[j]
else:
print direntries[j]
dirrecur(dlist) #recur again with full list of subdirectories.
#MAIN
abspath = os.getcwd()
direntries = dircache.opendir(abspath)
dirlist = []
for i in range(0,len(direntries)):
statresult = os.stat(direntries[i])
if (stat.S_ISDIR(statresult.st_mode) != 0): #if it's a directory file.
dirlist.append(direntries[i])
print direntries[i] #print it out too.
else:
print direntries[i]
dirrecur(dirlist)
It goes down to level 3 of the directory tree, but at one point I get the
error 2, the no such file or directory message. I have checked the
file permissions of the directory that it gets stuck on, and I have no idea why
it is gettintg stuck on that directory and not one of the directories before it.
I would be extremely grateful if someone could lend another set of eyes to my
code and see where I'm going wrong at, and why it goes to level 3 so to speak
of the tree and then fails.
I'm stumped!
Thanks so much in advance. Any and all input would be greatly appreciated!
Comment