#Author:'wbing' #-*-coding:utf-8-*- #递归简单的好,但是复杂的不建议,因为内存占用较大 import os def get_dir_file(path_dir): if not os.path.exists(path_dir): return dir_file_list = os.listdir(path_dir) #获取所有目录,文件 for name in dir_file_list: abs_path = os.path.join(path_dir,name) #拼接绝对路径 if os.path.isfile(abs_path): print('这是文件%s'%abs_path) if os.path.isdir(abs_path): print('这是目录%s'%abs_path) get_dir_file(abs_path) #如果是目录,则继续递归执行函数 if __name__ == '__main__': path = r'F:\Python3.6.0\day01' get_dir_file(path)