使用Python编写递归函数,遍历给定路劲中的文件夹和子目录,并将它们存储在数组中。下面是代码示例:
import os
def list_folders(path):
folders = []
for item in os.listdir(path):
item_path = os.path.join(path, item)
if os.path.isdir(item_path):
folders.append(item_path)
folders.extend(list_folders(item_path))
return folders
path = "/example/path" # 用实际路径替换
folders = list_folders(path)
print(folders)
该程序会输出给定路径下的所有文件夹和子目录列表。