import os existing_indexs = [] for root, dirs, files in os.walk("/data1/home/purui/projects/chatbot/kb"): for dir in dirs: existing_indexs.append(os.path.join(root, dir)) # 退出循环,因为我们只想要第一层的目录 break print(existing_indexs)