@@ -1577,30 +1577,30 @@ def modified(self, path):
15771577        raise  NotImplementedError 
15781578
15791579    def  tree (
1580-              self ,
1581-              path : str  =  '/' ,
1582-              recursion_limit : int  =  2 ,
1583-              max_display : int  =  25 ,
1584-              display_size : bool  =  False ,
1585-              prefix : str  =  "" ,
1586-              is_last : bool  =  True ,
1587-              first : bool  =  True ,
1588-              indent_size : int  =  4 
1580+         self ,
1581+         path : str  =  "/" ,
1582+         recursion_limit : int  =  2 ,
1583+         max_display : int  =  25 ,
1584+         display_size : bool  =  False ,
1585+         prefix : str  =  "" ,
1586+         is_last : bool  =  True ,
1587+         first : bool  =  True ,
1588+         indent_size : int  =  4 , 
15891589    ) ->  str :
15901590        """ 
15911591        Return a tree-like structure of the filesystem starting from the given path as a string. 
1592-      
1592+ 
15931593        Parameters 
15941594        ---------- 
15951595            path: Root path to start traversal from 
15961596            recursion_limit: Maximum depth of directory traversal 
15971597            max_display: Maximum number of items to display per directory 
15981598            display_size: Whether to display file sizes 
15991599            prefix: Current line prefix for visual tree structure 
1600-             is_last: Whether current item is last in its level              
1600+             is_last: Whether current item is last in its level 
16011601            first: Whether this is the first call (displays root path) 
16021602            indent_size: Number of spaces by indent 
1603-      
1603+ 
16041604        Returns 
16051605        ------- 
16061606            str: A string representing the tree structure. 
@@ -1613,6 +1613,7 @@ def tree(
16131613            >>> tree = fs.tree(display_size=True, recursion_limit=3, indent_size=8, max_display=10) 
16141614            >>> print(tree) 
16151615        """ 
1616+ 
16161617        def  format_bytes (n : int ) ->  str :
16171618            """Format bytes as text.""" 
16181619            for  prefix , k  in  (
@@ -1625,38 +1626,54 @@ def format_bytes(n: int) -> str:
16251626                if  n  >=  0.9  *  k :
16261627                    return  f"{ n  /  k :.2f} { prefix }  
16271628            return  f"{ n }  
1628-      
1629+ 
16291630        result  =  []
1630-      
1631+ 
16311632        if  first :
16321633            result .append (path )
1633-      
1634+ 
16341635        if  recursion_limit :
16351636            indent  =  " "  *  indent_size 
16361637            contents  =  self .ls (path , detail = True )
1637-             contents .sort (key = lambda  x : (not  x .get ('type' ) ==  'directory' , x .get ('name' , '' )))
1638-     
1638+             contents .sort (
1639+                 key = lambda  x : (x .get ("type" ) !=  "directory" , x .get ("name" , "" ))
1640+             )
1641+ 
16391642            if  max_display  is  not None  and  len (contents ) >  max_display :
16401643                displayed_contents  =  contents [:max_display ]
16411644                remaining_count  =  len (contents ) -  max_display 
16421645            else :
16431646                displayed_contents  =  contents 
16441647                remaining_count  =  0 
1645-      
1648+ 
16461649            for  i , item  in  enumerate (displayed_contents ):
1647-                 is_last_item  =  (i  ==  len (displayed_contents ) -  1 ) and  (remaining_count  ==  0 )
1648-     
1649-                 branch  =  "└"  +  ('─'  *  (indent_size  -  2 )) if  is_last_item  else  "├"  +  ('─'  *  (indent_size  -  2 ))
1650-                 branch  +=  ' ' 
1651-                 new_prefix  =  prefix  +  (indent  if  is_last_item  else  "│"  +  " "  *  (indent_size  -  1 ))
1652-     
1653-                 name  =  os .path .basename (item .get ('name' , '' ))
1654-                 
1655-                 if  display_size  and  item .get ('type' ) ==  'directory' :
1656-                     sub_contents  =  self .ls (item .get ('name' , '' ), detail = True )
1657-                     num_files  =  sum (1  for  sub_item  in  sub_contents  if  sub_item .get ('type' ) ==  'file' )
1658-                     num_folders  =  sum (1  for  sub_item  in  sub_contents  if  sub_item .get ('type' ) ==  'directory' )
1659-     
1650+                 is_last_item  =  (i  ==  len (displayed_contents ) -  1 ) and  (
1651+                     remaining_count  ==  0 
1652+                 )
1653+ 
1654+                 branch  =  (
1655+                     "└"  +  ("─"  *  (indent_size  -  2 ))
1656+                     if  is_last_item 
1657+                     else  "├"  +  ("─"  *  (indent_size  -  2 ))
1658+                 )
1659+                 branch  +=  " " 
1660+                 new_prefix  =  prefix  +  (
1661+                     indent  if  is_last_item  else  "│"  +  " "  *  (indent_size  -  1 )
1662+                 )
1663+ 
1664+                 name  =  os .path .basename (item .get ("name" , "" ))
1665+ 
1666+                 if  display_size  and  item .get ("type" ) ==  "directory" :
1667+                     sub_contents  =  self .ls (item .get ("name" , "" ), detail = True )
1668+                     num_files  =  sum (
1669+                         1  for  sub_item  in  sub_contents  if  sub_item .get ("type" ) ==  "file" 
1670+                     )
1671+                     num_folders  =  sum (
1672+                         1 
1673+                         for  sub_item  in  sub_contents 
1674+                         if  sub_item .get ("type" ) ==  "directory" 
1675+                     )
1676+ 
16601677                    if  num_files  ==  0  and  num_folders  ==  0 :
16611678                        size  =  " (empty folder)" 
16621679                    elif  num_files  ==  0 :
@@ -1665,32 +1682,34 @@ def format_bytes(n: int) -> str:
16651682                        size  =  f" ({ num_files } { 's'  if  num_files  >  1  else  '' }  
16661683                    else :
16671684                        size  =  f" ({ num_files } { 's'  if  num_files  >  1  else  '' } { num_folders } { 's'  if  num_folders  >  1  else  '' }  
1668-                 elif  display_size  and  item .get (' type' ==  ' file' 
1685+                 elif  display_size  and  item .get (" type" ==  " file" 
16691686                    size  =  f" ({ format_bytes (item .get ('size' , 0 ))}  
16701687                else :
16711688                    size  =  "" 
1672-      
1689+ 
16731690                result .append (f"{ prefix } { branch } { name } { size }  )
1674-      
1675-                 if  item .get (' type' ==  ' directory' and  recursion_limit  >  0 :
1691+ 
1692+                 if  item .get (" type" ==  " directory" and  recursion_limit  >  0 :
16761693                    result .append (
16771694                        self .tree (
1678-                             path = item .get (' name' ,  '' ),
1695+                             path = item .get (" name" ,  "" ),
16791696                            recursion_limit = recursion_limit  -  1 ,
16801697                            max_display = max_display ,
16811698                            display_size = display_size ,
16821699                            prefix = new_prefix ,
16831700                            is_last = is_last_item ,
16841701                            first = False ,
1685-                             indent_size = indent_size 
1702+                             indent_size = indent_size , 
16861703                        )
16871704                    )
1688-      
1705+ 
16891706            if  remaining_count  >  0 :
16901707                more_message  =  f"{ remaining_count }  
1691-                 result .append (f"{ prefix } { '└'  +  ('─'  *  (indent_size  -  2 ))} { more_message }  )
1692-     
1693-         return  "\n " .join ((_  for  _  in  result  if  _ ))
1708+                 result .append (
1709+                     f"{ prefix } { '└'  +  ('─'  *  (indent_size  -  2 ))} { more_message }  
1710+                 )
1711+ 
1712+         return  "\n " .join (_  for  _  in  result  if  _ )
16941713
16951714    # ------------------------------------------------------------------------ 
16961715    # Aliases 
0 commit comments