Is there any built in functions to find all the files under a particular directory including files under subdirectories ?
I have tried this code, but not working…may be the logic itself is wrong…
def fun(mydir):
lis=glob.glob(mydir)
length=len(lis)
l,i=0,0
if len(lis):
while(l+i<length):
if os.path.isfile(lis[i]):
final.append(lis[i])
lis.pop(i)
l=l+1
i=i+1
else:
i=i+1
print final
fun(lis)
else:
print final
There is no built-in function, but using
os.walkit’s trivial to construct it:ETA: the
os.walkfunction walks directory tree recursively; therecursive_file_genfunction is a generator (usesyieldkeyword to produce next file). To get the resulting list do: