https://www.jianshu.com/p/1038c6170775

import os
# 方法一: os.walk实现
def items_dir(rootname):
    l = []
    for main_dir, dirs, file_name_list in os.walk(rootname):
        # print('1',main_dir)
        # print('2',dirs)
        # print('3',file_name_list)
        for file in file_name_list:
            file_path = os.path.join(main_dir,file)
            print(file_path)
            l.append(file_path)
    return l

# items_dir('C:\\HXK\\code')

#===================================================#
#方法2 递归实现,os.listdir()
def list_all_files(rootdir):
    _files = []
    l1 = os.listdir(rootdir)# 列出文件夹下的所有目录和文件
    for i in range(0,len(l1)):
        path = os.path.join(rootdir, l1[i])
        if os.path.isdir(path):
            _files.extend(list_all_files(path))
        if os.path.isfile(path):
            _files.append(path)

    return _files

print(list_all_files('C:\\HXK\\code'))

  

摘自:https://www.jianshu.com/p/1038c6170775

posted @ 2019-12-15 14:19  LiuYanYGZ  阅读(334)  评论(0编辑  收藏  举报