Discuss / Python / 练习2用递归可以完全不用循环

练习2用递归可以完全不用循环

Topic source

遥望君山

#1 Created at ... [Delete] [Delete and Lock User]
import os
import sys

def find(dirs, s, parent):
    paths = []
    if not dirs:
        return paths

    sub_node = os.path.join(parent, dirs[0])
    if os.path.isdir(sub_node):
        paths = find(os.listdir(sub_node), s, sub_node)
    elif s in dirs[0]:
        paths = [sub_node]
    return paths + find(dirs[1:], s, parent)

print(find(os.listdir('.'), sys.argv[1], '.'))


  • 1

Reply