python - Recursively list all files in directory (Unix) -
i trying list files directory recursively using python. saw many solutions using os.walk
. don't want use os.walk
. instead want implement recursion myself.
import os fi = [] def files(a): f = [i in os.listdir(a) if os.path.isfile(i)] if len(os.listdir(a)) == 0: return if len(f) > 0: fi.extend(f) j in [i in os.listdir(a) if os.path.isdir(i)]: files(j) files('.') print fi
i trying learn recursion. saw following q?a, not able implement correctly in code.
os.listdir
return filename (without full path) think calling files(j)
not work correctly.
try using files(os.path.join(dirname,j))
or this:
def files(a): entries = [os.path.join(a,i) in os.listdir(a)] f = [i in entries if os.path.isfile(i)] if len(os.listdir(a)) == 0: return if len(f) > 0: fi.extend(f) j in [i in entries if os.path.isdir(i)]: files(j)
i tried stay close structure. however, write 1 loop on entries, that:
def files(a): entries = [os.path.join(a,i) in os.listdir(a)] if len(entries) == 0: return e in entries: if os.path.isfile(e): fi.append(e) elif os.path.isdir(e): files(e)
Comments
Post a Comment