遍历获取一个路径下所有子文件夹及文件
import os
def get_dir_file(rootdir):
objects = os.listdir(rootdir)
dir_list = []
file_list = []
for obj in objects:
judge_dir = os.path.isdir(os.path.join(rootdir, obj))
if judge_dir:
dir_list.append(os.path.join(rootdir, obj))
else:
file_list.append(os.path.join(rootdir, obj))
if len(dir_list)>0:
for dir_list_i in dir_list:
file_list_i = get_dir_file(dir_list_i)[1]
for file_i in file_list_i:
file_list.append(file_i)
return (dir_list,file_list)
def main():
path = '/zy_DATA/zy/test'
result = get_dir_file(path)
print(result[0])
print(result[1])
if __name__ == "__main__":
main()