爱好者cgglzpy为micropython添加了一个tree命令,查看文件列表比较方便。
class TREE:
def __repr__(self):
self.__call__()
return ""
def __call__(self, path=".", level=0, is_last=False, is_root=True,
carrier=" "):
l = os.listdir(path)
nf = len([file for file in os.listdir(path) if not os.stat(file)[0] & 0x4000])
nd = len(l) - nf
ns_f, ns_d = 0, 0
l.sort()
if len(l) > 0:
last_file = l[-1]
else:
last_file = ''
for f in l:
st = os.stat("%s/%s" % (path, f))
if st[0] & 0x4000: # stat.S_IFDIR
print(self._treeindent(level, f, last_file, is_last=is_last, carrier=carrier) + " %s <dir>" % f)
os.chdir(f)
level += 1
lf = last_file == f
if level > 1:
if lf:
carrier += " "
else:
carrier += " │"
ns_f, ns_d = self.__call__(level=level, is_last=lf,
is_root=False, carrier=carrier)
if level > 1:
carrier = carrier[:-5]
os.chdir('..')
level += (-1)
nf += ns_f
nd += ns_d
else:
print(self._treeindent(level, f, last_file, is_last=is_last, carrier=carrier) + " %s" % (f))
if is_root:
print('{} directories, {} files'.format(nd, nf))
else:
return (nf, nd)
def _treeindent(self, lev, f, lastfile, is_last=False, carrier=None):
if lev == 0:
return ""
else:
if f != lastfile:
return carrier + " ├────"
else:
return carrier + " └────"
tree = TREE()
|