This recipe is a simple solution for turning a recursive function into a non-recursive function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
import os def recursive(path): """Move through all files, directories, and subdirectories of a path""" yield path for name in os.listdir(path): fullpath = os.path.join(path, name) if os.path.isdir(fullpath): for subpath in recursive(fullpath): yield subpath else: yield fullpath def nonrecursive(path): """Move through all files, directories, and subdirectories of a path""" paths = [path] while paths: dpath = paths.pop(0) yield dpath for name in os.listdir(dpath): fullpath = os.path.join(path, name) if os.path.isdir(fullpath): paths.append(fullpath) else: yield fullpath
Recursive can mean calling a function within a function. So if you have function
func and you call
func within itself, your function is recursive. Sometimes you have a problem where the easiest way to solve it is to make the function act this way. This can lead to problems where Python gives you an error if you recur to deeply.
A solution to this would be to use a list. A list of items which need to be run through the same code again. By wrapping the code to run again in a loop, the task of running through all the objects in that list becomes trivial.