在Python中,你可以使用os
模块的os.walk()
函数来查看目录结构
import os def print_directory_structure(path): for root, dirs, files in os.walk(path): level = root.replace(path, '').count(os.sep) indent = ' ' * 4 * (level) print(f'{indent}[{os.path.basename(root)}]') sub_indent = ' ' * 4 * (level + 1) for f in files: print(f'{sub_indent}{f}') directory_path = 'your_directory_path_here' # Replace with the path you want to explore print_directory_structure(directory_path)
将your_directory_path_here
替换为你要查看的目录路径。这个函数将打印出目录结构,包括子目录和文件。