zoukankan      html  css  js  c++  java
  • 【python】递归

    #自己调自己,默认递归深度是1000,实际测试997次,998会报错

    def func(count):

        print("我是谁" +str(count))

        func(count+1)

    func(1)

     

    #修改递归深度,但不一定可以到设置的值

    import sys

    sys.setrecursionlimit(10000) 

    应用场景:遍历树形结构

    import  os

    filePath = "d:sylarpython_workspace"

    def read(filePath, n):

        it = os.listdir(filePath)   # 打开文件夹

        for el in it:

            #print(el) #获取的是文件夹和文件的名字

            #获取到文件夹文件的路径

            fp = os.path.join(filePath, el) # 获取到绝对路径

            if os.path.isdir(fp):   # 判断是否是文件夹

                print(" "*n,el)  #打印文件夹名

                read(fp, n+1)    # 又是文件夹. 继续读取内部的内容 递归入口

            else:

                print(" "*n,el)    # 递归出口

    read(filePath, 0)

  • 相关阅读:
    设计模式(1)-行为类
    rocketmq(1)
    zookeeper(2)-curator
    Spring之HandlerInterceptor拦截器
    云之家如何获取登录用户信息?
    KDTable如何添加合计行?
    财务报表如何直接取数?
    DEP脚本
    Mybatis之关联查询及动态SQL
    Mybatis之XML、注解
  • 原文地址:https://www.cnblogs.com/xlzhangq/p/13211192.html
Copyright © 2011-2022 走看看