@@ -1577,10 +1577,10 @@ def tree(
15771577 is_last : bool = True ,
15781578 first : bool = True ,
15791579 indent_size : int = 4
1580- ):
1580+ ) -> str :
15811581 """
1582- Display a tree-like structure of the filesystem starting from the given path.
1583-
1582+ Return a tree-like structure of the filesystem starting from the given path as a string .
1583+
15841584 Parameters
15851585 ----------
15861586 path: Root path to start traversal from
@@ -1591,11 +1591,16 @@ def tree(
15911591 is_last: Whether current item is last in its level
15921592 first: Whether this is the first call (displays root path)
15931593 indent_size: Number of spaces by indent
1594-
1594+
1595+ Returns
1596+ -------
1597+ str: A string representing the tree structure.
1598+
15951599 Example
15961600 -------
1597- >>> fs.tree(path='/start/folder', display_size=True)
1598-
1601+ >>> tree = fs.tree(path='/start/folder', display_size=True)
1602+ >>> print(tree)
1603+
15991604 /start/folder
16001605 ├── folder1
16011606 │ ├── file1.txt (1.234MB)
@@ -1612,49 +1617,58 @@ def format_bytes(n: int) -> str:
16121617 ("M" , 2 ** 20 ),
16131618 ("k" , 2 ** 10 ),
16141619 ):
1615- if n >= 0.9 * k :
1620+ if n >= 0.9 * k :
16161621 return f"{ n / k :.2f} { prefix } b"
16171622 return f"{ n } B"
1618-
1623+
1624+ result = []
1625+
16191626 if first :
1620- print (path )
1627+ result .append (path )
1628+
16211629 if recursion_limit :
16221630 indent = " " * indent_size
16231631 contents = self .ls (path , detail = True )
16241632 contents .sort (key = lambda x : (not x .get ('type' ) == 'directory' , x .get ('name' , '' )))
1625-
1633+
16261634 if max_display is not None and len (contents ) > max_display :
16271635 displayed_contents = contents [:max_display ]
16281636 remaining_count = len (contents ) - max_display
16291637 else :
16301638 displayed_contents = contents
16311639 remaining_count = 0
1632-
1640+
16331641 for i , item in enumerate (displayed_contents ):
16341642 is_last_item = (i == len (displayed_contents ) - 1 ) and (remaining_count == 0 )
1635-
1636- branch = "└" + ('─' * (indent_size - 2 )) if is_last_item else "├" + ('─' * (indent_size - 2 ))
1643+
1644+ branch = "└" + ('─' * (indent_size - 2 )) if is_last_item else "├" + ('─' * (indent_size - 2 ))
16371645 branch += ' '
16381646 new_prefix = prefix + (indent if is_last_item else "│" + " " * (indent_size - 1 ))
1639-
1647+
16401648 name = os .path .basename (item .get ('name' , '' ))
16411649 size = f" ({ format_bytes (item .get ('size' , 0 ))} )" if display_size and item .get ('type' ) == 'file' else ""
1642-
1643- print (f"{ prefix } { branch } { name } { size } " )
1644-
1650+
1651+ result . append (f"{ prefix } { branch } { name } { size } " )
1652+
16451653 if item .get ('type' ) == 'directory' and recursion_limit > 0 :
1646- self .tree (path = item .get ('name' , '' ),
1647- recursion_limit = recursion_limit - 1 ,
1648- max_display = max_display ,
1649- display_size = display_size ,
1650- prefix = new_prefix ,
1651- is_last = is_last_item ,
1652- first = False ,
1653- indent_size = indent_size )
1654-
1654+ result .append (
1655+ self .tree (
1656+ path = item .get ('name' , '' ),
1657+ recursion_limit = recursion_limit - 1 ,
1658+ max_display = max_display ,
1659+ display_size = display_size ,
1660+ prefix = new_prefix ,
1661+ is_last = is_last_item ,
1662+ first = False ,
1663+ indent_size = indent_size
1664+ )
1665+ )
1666+
16551667 if remaining_count > 0 :
16561668 more_message = f"{ remaining_count } more item(s) not displayed."
1657- print (f"{ prefix } { '└── ' if is_last else '├── ' } { more_message } " )
1669+ result .append (f"{ prefix } { '└── ' if is_last else '├── ' } { more_message } " )
1670+
1671+ return "\n " .join ((_ for _ in result if _ ))
16581672
16591673 # ------------------------------------------------------------------------
16601674 # Aliases
0 commit comments