全国绿色计算大赛(第一阶段) 模拟赛python 第2关:文件查看器

文件查看器

需要用到简单的递归原理

Python使用print的效果,代码量有效减少

import os, sys

class Task:
    def showDirTree(self,path):
        
########## BEGIN ##########
#递归    
        def list_search(path_1,num):
    
            
            if os.path.isdir(path_1):
                print(' '*num+'+--'+path_1.split('/')[-1])

                for i in os.listdir(path_1):
                    list_search(path_1+'/'+i,num+2)
            else:
                print(' '*num+'--'+path_1.split('/')[-1])       
        
        list_search(path,0)

########## END ##########

猜你喜欢

转载自blog.csdn.net/qq_36440163/article/details/83449817