获取某文件夹下的文件,递归

 

import os

def show_all(path, num=0):
    """
    递归打印传入目录下的目录和文件
    :param path: str
    :param num: int
    :return:
    """
    path_list = os.listdir(path)
    if path_list:
        for i in path_list:
            inner_path = os.path.join(path, i)
            if os.path.isdir(inner_path):
                print('    '*num, i)
                show_all(inner_path,num+1)
            else:
                print('    '*num, i)

path = os.getcwd()
show_all(path)

 

 

 

 

 

import os
def Test2(rootDir):
    paths=os.listdir(rootDir)
    for lis in paths:
        path=os.path.join(rootDir,lis)
        print(path)
        if os.path.isdir(path):
             Test2(path)

Test2(r'C:\Users\yijue_lu\Desktop\python相关书籍')

 

posted @ 2019-07-17 10:12  yijue_lu  阅读(181)  评论(0编辑  收藏  举报