zoukankan      html  css  js  c++  java
  • python基础 day21 递归函数、shutil模块

    一、递归函数

    • 递归的最大深度:1000层
    def func():
        print(111)
        func()
        print(222)
    func()
    
    • 设置递归的最大深度
    import sys
    sys.setrecursionlimit(100000)
    
    • 递归函数如何停下来
    count = 0
    def func():
        global count
        count += 1
        print(111)
        if count == 3:
            return
        func()
        print(222)
    func()
    '''结果为:
    111
    111
    111
    222
    222'''
    

    练习

    # 1.计算阶乘
    def func(s):
        if s == 1:
            return s
        else:
            return s * func(s-1)
    
    print(func(10))
    
    # 2.os模块查看一个文件夹下的所有文件,这个文件夹下还有文件,不能用walk
    # D:python_learn
    import os
    def show_file(path):
        file = os.listdir(path)
        for i in file:
            abs_path = os.path.join(path, i)
            if os.path.isfile(abs_path):
                print(i)
            elif os.path.isdir(abs_path):
                print(f'-----{abs_path}-----')
                show_file(abs_path)
    show_file('D:python_learn学习代码')
    
    # 3.os模块计算一个文件夹下所有文件的大小,这个文件夹下还有文件夹
    import os
    def file_size(path):
        file_list = os.listdir(path)
        size = 0
        for name in file_list:
            abs_path = os.path.join(path, name)
            if os.path.isfile(abs_path):
                size += os.stat(abs_path).st_size
            elif os.path.isdir(abs_path):
                ret = file_size(abs_path)
                size += ret
        return size
    size_all = file_size('D:python_learn学习代码')
    print(size_all)
    
    # 4.计算斐波那契数列
    # 1 1 2 3 5 8 13 21 34 55....
    # # 方法一:递归函数
    '''
    这种递归函数调用了效率极差
    def fib(n):
        if n == 1 or n ==2:
            return 1
        else:
            return (fib(n-1) + fib(n-2))
    print(fib(10))
    '''
    # 正确方法
    def fib(n,a=1, b=1):
        if n == 1 or n == 2:
            return b
        else:
            return fib(n-1, a=b, b=a+b)
    print(fib(10))
    
    
    # # 方法二:循环
    def fib(n):
        a = 1
        b = 1
        while n>2:
            a, b = b, a+b
            n -=1
        return b
    print(fib(10))
    
    # 5.三级菜单
    menu = {
        '江苏':
            {
                '徐州':
                    {
                       '沛县': {},
                       '丰县': {},
                       '泉山': {}
                    },
                '无锡':
                    {
                        '新吴': {},
                        '惠山': {},
                        '梁溪': {}
                    }
               },
        '上海':
            {
                '宝山':
                    {
                        '码头': {},
                        '家乐福': {}
                    },
                '浦东':
                    {
                        '迪士尼': {},
                        '机场': {}
                    }
            }
    }
    
    def menu_func(menu):
        while True:
            for city in menu:
                print(city)
            choice = input('>>>').strip()
            if menu.get(choice):
                dic = menu.get(choice)
                ret = menu_func(dic)
                if not ret: return False
            elif choice.upper() == 'Q':
                print('bye')
                return False
            elif choice.upper() == 'B':
                return True
    
    menu_func(menu)
    

    二、shutil模块

    # shutil 与文件目录相关的模块
    import shutil
    
    # # 拷贝文件
    # shutil.copy2(r'D:python_learn学习代码day21aloha.txt', r'D:python_learn学习代码day21aloha2.txt')
    #
    # # 拷贝目录
    # shutil.copytree(r'D:python_learn学习代码day21aloha', r'D:python_learn学习代码day21aloha2')
    # # 可以忽略指定的文件不拷贝
    # shutil.copytree(r'aloha', r'aloha3', ignore=shutil.ignore_patterns('111.txt'))
    #
    # # 删除文件夹
    # shutil.rmtree('aloha2', ignore_errors=True)
    
    # # 移动文件/目录
    # shutil.move('aloha', 'aloha2/aloha3', copy_function=shutil.copy2)
    
    # # 获取磁盘使用空间
    # total, used, free = shutil.disk_usage('C:\')
    # print(f'当前共{total//1073741824}GB, 使用{used//1073741824}GB,剩余{free//1073741824}GB')
    
    # # 压缩文件
    # shutil.make_archive('压缩文件夹名字', 'zip', '待压缩的文件夹路径')
    
    # # 解压文件
    # shutil.unpack_archive('zip文件的路径.zip', '解压到目的文件夹路径')
    
  • 相关阅读:
    次小生成树模板(poj1679)
    ISAP模板
    ZOJ3781
    Uva12663
    LightOJ1089
    网络流DINIC模板
    FZU2030(括号匹配)
    NOIP2011提高组(选择客栈)
    DRF之视图家族
    DRF多表设计与ModelSerializer组件
  • 原文地址:https://www.cnblogs.com/west-yang/p/12775297.html
Copyright © 2011-2022 走看看