内置函数lambda,sorted,filter,map,函数递归,递归实现文件的遍历,计算文件夹的大小,二分法查找

#递归实现文件的遍历
import os
def func(path,ceng=0):
    name_list=os.listdir(path)   #查看路径下所有文件
    for name in name_list:
        path_abs=os.path.join(path,name) #拼接路径
        if os.path.isdir(path_abs):  #判断是否为文件夹
            print('\t'*ceng,name)
            func(path_abs,ceng+1)   #是文件夹递归
        else:
            print('\t'*ceng,name)   #不是文件夹打印文件名
    # else:
    #     return
func('C:\python')

#递归实现文件夹大小的计算
import os
def func(path):
  size=0
  list_name=os.listdir(path)
  for name in lisr_name:
    path_abs=os.path.jion(path,name)
    if os.path.isfile(path_abs):
      size+=os.path.getsize(path_abs)
    elif os.path.isdir(path_abs):
      ret=func(path_abs)
      size+=ret
  return size
print('此文件夹大小为:',func(路径))

#二分法查找 #1 变索引 循环 lst=[8,5,6,2,3,1,9,0,7,4] lst=sorted(lst) print(lst) n=int(input('请输入一个数字:')) left=0 right=len(lst)-1 while left <= right: mid=(left+right)//2 if n<lst[mid]: right=mid-1 elif n>lst[mid]: left=mid+1 else: print('找到了') break else: print('不存在') #2 切列表 递归 lst=[8,5,6,2,3,1,9,0,7,4] lst=sorted(lst) def func(n,lst): left=0 right=len(lst)-1 mid=(left+right)//2 if left<=right: if n >lst[mid]: new_lst=lst[mid+1:] return func(n,new_lst) elif n< lst[mid]: new_lst=lst[:mid] return func(n,new_lst) else: return True else: return False print(func(19,lst)) #3 变索引 递归 lst=[8,5,6,2,3,1,9,0,7,4] lst=sorted(lst) def func(n,lst,left=0,right=None): if right==None: right=len(lst)-1 mid=(left+right)//2 if left <= right: if n <lst[mid]: right=mid-1 return func(n,lst,left,right) elif n>lst[mid]: left=mid+1 return func(n,lst,left,right) else: return True else: return False print(func(13,lst))

  

 

posted @ 2018-09-20 16:59  冒蓝火的加特林哒哒哒  阅读(300)  评论(0编辑  收藏  举报