温馨提示×

python中cd命令怎么查看目录结构

小樊
81
2024-12-09 12:28:04
栏目: 编程语言

在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替换为你要查看的目录路径。这个函数将打印出目录结构,包括子目录和文件。

0