Recursive directory Generator mode, else in the TMP display to obtain yield is indispensable, in the recursive algorithm to use the generator, you need to explicitly get all yield values in the generator's original function (first call)
def get_file_recur(path): children = os.listdir(path) for child in children: qualified_child = os.path.join(path,child) if os.path.isfile(qualified_child): yield qualified_child else: tmp = get_file_recur(qualified_child) for item in tmp: yield itemfor file in get_file_recur('/home/xxx/xxx/Frank Li'): print(file)
Reference sources, as follows Flattern list
def flattern(lst): for item in lst: if isinstance(item,list): inner_list = flattern(item) for i in inner_list: yield i else: yield iteml=[1,2,3,4,5,[6],[7,8,[9,[10]]]]lst=flattern(l)print(list(lst))
Python Advanced Section Essence--those books won't tell you the pit