# coding=gbk
# encoding:utf-8
# -*- coding:gb2312 -*-
import os
import os.path
def dir_tree(path,sub_tree):
if sub_tree == 0:
print(path) # 输出第一级目录
path_tree = os.listdir(path) #获取当前目录下的文件和目录
for item in path_tree:
if '.git' not in item:
print("| " * sub_tree +"|___"+item)
with open("txt.txt", "a") as file:
file.write("| " * sub_tree +"|___"+item + "\n")
subtree= path+'\\'+item
if os.path.isdir(subtree): #判断是否为目录
dir_tree(subtree,sub_tree+1) #递归深度优先遍历
if __name__ == '__main__':
basepath = r"D:\0_me_python\其他_练习\语义分割"
dir_tree(basepath,0)
利用python获取目录树信息
最新推荐文章于 2024-09-25 19:07:28 发布