python中cd命令怎么查看目录结构
导读:在Python中,你可以使用os模块的os.walk( 函数来查看目录结构 import os def print_directory_structure(path : for root, dirs, files in os.wa...
在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
替换为你要查看的目录路径。这个函数将打印出目录结构,包括子目录和文件。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: python中cd命令怎么查看目录结构
本文地址: https://pptw.com/jishu/711521.html