最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python樹狀打印項(xiàng)目路徑的實(shí)現(xiàn)

 更新時(shí)間:2023年10月16日 08:33:50   作者:臨風(fēng)而眠  
在Python中,要打印當(dāng)前路徑,可以使用os模塊中的getcwd()函數(shù),本文主要介紹了python樹狀打印項(xiàng)目路徑,具有一定的參考價(jià)值,感興趣的可以了解一下

學(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)文章

  • python 連接各類主流數(shù)據(jù)庫的實(shí)例代碼

    python 連接各類主流數(shù)據(jù)庫的實(shí)例代碼

    下面小編就為大家分享一篇python 連接各類主流數(shù)據(jù)庫的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • Python如何獲取pid和進(jìn)程名字

    Python如何獲取pid和進(jìn)程名字

    這篇文章主要介紹了Python如何獲取pid和進(jìn)程名字的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • python pyppeteer 破解京東滑塊功能的代碼

    python pyppeteer 破解京東滑塊功能的代碼

    這篇文章主要介紹了python pyppeteer 破解京東滑塊功能的代碼,代碼簡單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • Pytorch釋放顯存占用方式

    Pytorch釋放顯存占用方式

    今天小編就為大家分享一篇Pytorch釋放顯存占用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • Python使用docx模塊編輯Word文檔

    Python使用docx模塊編輯Word文檔

    docx提供了一組功能豐富的函數(shù)和方法,用于創(chuàng)建、修改和讀取Word文檔,Python可以用它對(duì)word文檔進(jìn)行大批量的編輯,下面小編就來通過一些示例為大家好好講講吧
    2023-07-07
  • Windows下快速配置Python+OpenCV環(huán)境指南

    Windows下快速配置Python+OpenCV環(huán)境指南

    本文介紹了如何在Windows下使用uv工具快速配置Python環(huán)境并安裝OpenCV,步驟包括安裝Python、uv工具、創(chuàng)建虛擬環(huán)境、安裝OpenCV及其擴(kuò)展,并驗(yàn)證安裝,感興趣的朋友跟隨小編一起看看吧
    2026-01-01
  • opencv 形態(tài)學(xué)變換(開運(yùn)算,閉運(yùn)算,梯度運(yùn)算)

    opencv 形態(tài)學(xué)變換(開運(yùn)算,閉運(yùn)算,梯度運(yùn)算)

    這篇文章主要介紹了opencv 形態(tài)學(xué)變換(開運(yùn)算,閉運(yùn)算,梯度運(yùn)算),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 使用Python代碼識(shí)別股票價(jià)格圖表模式實(shí)現(xiàn)

    使用Python代碼識(shí)別股票價(jià)格圖表模式實(shí)現(xiàn)

    這篇文章主要為大家介紹了使用Python代碼識(shí)別股票價(jià)格圖表模式的實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • Python迭代器的應(yīng)用場景分析

    Python迭代器的應(yīng)用場景分析

    這篇文章給大家介紹Python迭代器的應(yīng)用場景分析,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2026-02-02
  • 基于python代碼批量處理圖片resize

    基于python代碼批量處理圖片resize

    這篇文章主要介紹了基于python代碼批量處理圖片resize,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06

最新評(píng)論

邓州市| 襄汾县| 竹山县| 内丘县| 曲水县| 南部县| 阳春市| 广东省| 商都县| 成都市| 呼伦贝尔市| 张掖市| 安多县| 屯留县| 井冈山市| 沁水县| 望奎县| 广宁县| 成安县| 静安区| 讷河市| 教育| 黎城县| 同仁县| 台江县| 南宁市| 奉化市| 阿城市| 安溪县| 沽源县| 六盘水市| 宜春市| 大洼县| 自治县| 会宁县| 错那县| 萍乡市| 鹿泉市| 莱阳市| 会同县| 大厂|