#获取文件路径中存在几个\ 用于构建结果时的树状缩进显示
n=path.count('\\')
dn=os.path.split(path)
#打印文件夹名称 这里用到了填充函数,设定打印字符串长度为文件长度+文件几级(每级填充两个 “·”)
print(dn[1].rjust(len(dn[1])+(n-1)*2,"·"))
for x in os.listdir(path):
p=os.path.join(path,x)
if os.path.isdir(p):
#文件夹就递归
forDirAndFile(p,fiter)
else:
if fiter in p:
#打印文件名称 这里用到了填充函数
name=x.rjust(len(x)+n*2,"·")
print(name)
forDirAndFile(".",".")
结果如下(这样层级结构很清晰):
.
learning.py
Model
··Test
····demo.py
··test.py
Python运行助手.bat
南人北相帅
我都是在windows下开发,直接做第二题 这题不用看,如果单单是这节的课,肯定需要用到地柜,当然不是排除第三方插件就直接有这个功能只是自己不知晓 贴上代码:
晕死作业格式不友好啊
import os
#path为路径,filter为过滤字符
def forDirAndFile(path,fiter):
forDirAndFile(".",".")
结果如下(这样层级结构很清晰): . learning.py Model ··Test ····demo.py ··test.py Python运行助手.bat