python樹狀打印項(xiàng)目路徑的實(shí)現(xiàn)
學(xué)習(xí)這個(gè)的需求來自于,我想把項(xiàng)目架構(gòu)告訴gpt問問它,然后不太會(huì)打印項(xiàng)目架構(gòu)?? 聯(lián)想到Linux的tree指令
import os class DirectoryTree: def __init__(self, path): self.path = path def print_tree(self, method='default'): if method == 'default': self._print_dir_tree(self.path) elif method == 'with_count': self._print_dir_tree_with_count(self.path) elif method == 'files_only': self._print_files_only(self.path) elif method == 'with_symbols': self._print_dir_tree_with_symbols(self.path) else: print("Invalid method!") def _print_dir_tree(self, path, indent=0): print(' ' * indent + '|---' + os.path.basename(path)) for item in os.listdir(path): itempath = os.path.join(path, item) if os.path.isdir(itempath): self._print_dir_tree(itempath, indent + 2) else: print(' ' * (indent + 2) + '|---' + item) def _print_dir_tree_with_count(self, path, indent=0): print(' ' * indent + '|---' + os.path.basename(path), end=' ') items = os.listdir(path) dirs = sum(1 for item in items if os.path.isdir( os.path.join(path, item))) files = len(items) - dirs print(f"(dirs: {dirs}, files: {files})") for item in items: itempath = os.path.join(path, item) if os.path.isdir(itempath): self._print_dir_tree_with_count(itempath, indent + 2) else: print(' ' * (indent + 2) + '|---' + item) def _print_files_only(self, path, indent=0): for item in os.listdir(path): itempath = os.path.join(path, item) if os.path.isdir(itempath): self._print_files_only(itempath, indent) else: print(' ' * indent + '|---' + item) def _print_dir_tree_with_symbols(self, path, indent=0): print(' ' * indent + '?? ' + os.path.basename(path)) for item in os.listdir(path): itempath = os.path.join(path, item) if os.path.isdir(itempath): self._print_dir_tree_with_symbols(itempath, indent + 2) else: print(' ' * indent + '?? ' + item) class EnhancedDirectoryTree: def __init__(self, path): self.path = path self.show_hidden = False self.depth_limit = None self.show_files = True self.show_dirs = True def display_settings(self, show_hidden=False, depth_limit=None, show_files=True, show_dirs=True): self.show_hidden = show_hidden self.depth_limit = depth_limit self.show_files = show_files self.show_dirs = show_dirs def print_tree(self): self._print_dir(self.path) def _print_dir(self, path, indent=0, depth=0): if self.depth_limit is not None and depth > self.depth_limit: return if self.show_dirs: print(' ' * indent + '?? ' + os.path.basename(path)) for item in sorted(os.listdir(path)): # Check for hidden files/folders if not self.show_hidden and item.startswith('.'): continue itempath = os.path.join(path, item) if os.path.isdir(itempath): self._print_dir(itempath, indent + 2, depth + 1) else: if self.show_files: print(' ' * indent + '?? ' + item) # 創(chuàng)建類的實(shí)例 tree = DirectoryTree(os.getcwd()) # 使用默認(rèn)方法打印 print("Default:") tree.print_tree() # 使用帶計(jì)數(shù)的方法打印 print("\nWith Count:") tree.print_tree(method='with_count') # 使用只打印文件的方法 print("\nFiles Only:") tree.print_tree(method='files_only') # 使用emoji print("\nWith Symbols:") tree.print_tree(method='with_symbols') print('================================================================') enhancedtree = EnhancedDirectoryTree(os.getcwd()) # Default print enhancedtree.print_tree() print("\n---\n") # Configure settings to show hidden files, but only up to depth 2 and only directories enhancedtree.display_settings(show_hidden=True, depth_limit=2, show_files=False) enhancedtree.print_tree() print("\n---\n") # Configure settings to show only files up to depth 1 enhancedtree.display_settings(show_files=True, show_dirs=False, depth_limit=1) enhancedtree.print_tree()
os.getcwd()
: 返回當(dāng)前工作目錄的路徑名。os.path.basename(path)
: 返回路徑名path
的基本名稱,即去掉路徑中的目錄部分,只保留文件或目錄的名稱部分。os.listdir(path)
: 返回指定路徑path
下的所有文件和目錄的名稱列表。os.path.isdir(path)
: 判斷路徑path
是否為一個(gè)目錄,如果是則返回True
,否則返回False
。os.path.join(path, *paths)
: 將多個(gè)路徑組合成一個(gè)完整的路徑名。這個(gè)函數(shù)會(huì)自動(dòng)根據(jù)操作系統(tǒng)的不同使用不同的路徑分隔符。上面兩個(gè)類里面的函數(shù)其實(shí)就是用到遞歸,第二個(gè)類的那個(gè)函數(shù)可以設(shè)置遞歸深度
到此這篇關(guān)于python樹狀打印項(xiàng)目路徑的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)python樹狀打印路徑內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
簡(jiǎn)單幾步教你學(xué)會(huì)Python接口自動(dòng)化測(cè)試
這篇文章主要介紹了簡(jiǎn)單幾步教你學(xué)會(huì)Python接口自動(dòng)化測(cè)試,本文從一個(gè)簡(jiǎn)單的登錄接口測(cè)試入手,一步步調(diào)整優(yōu)化接口調(diào)用姿勢(shì),期望讀者可以通過本文對(duì)接口自動(dòng)化測(cè)試有一個(gè)大致的了解,需要的朋友可以參考下2023-08-08一個(gè)基于flask的web應(yīng)用誕生 使用模板引擎和表單插件(2)
一個(gè)基于flask的web應(yīng)用誕生第二篇,這篇文章主要介紹了如何使用jinja2模板引擎和wtf表單插件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04Django如何利用uwsgi和nginx修改代碼自動(dòng)重啟
這篇文章主要介紹了Django如何利用uwsgi和nginx修改代碼自動(dòng)重啟問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05python?Pangu自動(dòng)美化中文排版工具使用探索
這篇文章主要為大家介紹了python?Pangu自動(dòng)美化中文排版工具使用探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01如何在Python?中使用?join()?函數(shù)把列表拼接成一個(gè)字符串
這篇文章主要介紹了如何在Python?中使用?join()?函數(shù)把列表拼接成一個(gè)字符串,文章圍繞?join()?函數(shù)的相關(guān)資料展開詳細(xì)內(nèi)容,需要的小伙伴可以參考一下,希望對(duì)你有幫助2022-03-03Keras模型轉(zhuǎn)成tensorflow的.pb操作
這篇文章主要介紹了Keras模型轉(zhuǎn)成tensorflow的.pb操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07解決Pandas生成Excel時(shí)的sheet問題的方法總結(jié)
估計(jì)有不少小伙伴在將 DataFrame導(dǎo)入到Excel的時(shí)候,遇到過下面這種尷尬的情況:想給一個(gè)現(xiàn)有的Excel文件追加一個(gè)sheet,結(jié)果發(fā)現(xiàn)其它的sheet都沒了等,本文就來告訴你如何解決這些問題2022-08-08