I have the following to recursively list files in a directory:
import os
def listdir(dir):
items = [dir + '/' + item for item in os.listdir(dir)]
for item in items:
if not os.path.isdir(item):
print (item)
else:
listdir(item)
As a sort of academic exercise I wanted to see how the above could be converted into a loop without recursion. What would be an example of doing that? So far I had something along the lines of:
def listdir(dir):
while True:
items = os.listdir(dir)
for item in items:
if not is.path.isdir(item):
print (item)
else:
# ?