import os
for child in os.listdir(dir_path):
child_path = os.path.join(dir_path, child)
if os.path.isdir(child_path):
print(child_path)
遍历所有目录和文件(os.walk)
import os
# 遍历并打印所有目录和文件名称
for root_dir, sub_dirs, files in os.walk(dir_path):
for sub_dir in sub_dirs:
print(os.path.join(root_dir, sub_dir))
for file in files:
print(os.path.join(root_dir, file))
获取目录下的所有文件
def traverse_dir(dir_path, all_files=[]):
if os.path.isdir(dir_path):
for child in os.listdir(dir_path):
child_path = os.path.join(dir_path, child)
traverse_dir(child_path, all_files)
else:
all_files.append(dir_path)