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

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

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

學(xué)習(xí)這個的需求來自于,我想把項目架構(gòu)告訴gpt問問它,然后不太會打印項目架構(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)建類的實例
tree = DirectoryTree(os.getcwd())

# 使用默認(rèn)方法打印
print("Default:")
tree.print_tree()

# 使用帶計數(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 是否為一個目錄,如果是則返回 True,否則返回 False。

  • os.path.join(path, *paths): 將多個路徑組合成一個完整的路徑名。這個函數(shù)會自動根據(jù)操作系統(tǒng)的不同使用不同的路徑分隔符。

  • 上面兩個類里面的函數(shù)其實就是用到遞歸,第二個類的那個函數(shù)可以設(shè)置遞歸深度

到此這篇關(guān)于python樹狀打印項目路徑的實現(xiàn)的文章就介紹到這了,更多相關(guān)python樹狀打印路徑內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

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

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

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

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

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

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

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

    Pytorch釋放顯存占用方式

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

    Python使用docx模塊編輯Word文檔

    docx提供了一組功能豐富的函數(shù)和方法,用于創(chuàng)建、修改和讀取Word文檔,Python可以用它對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及其擴展,并驗證安裝,感興趣的朋友跟隨小編一起看看吧
    2026-01-01
  • opencv 形態(tài)學(xué)變換(開運算,閉運算,梯度運算)

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

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

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

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

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

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

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

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

最新評論

鹿邑县| 兴和县| 通城县| 田阳县| 霍州市| 如皋市| 仙居县| 宣汉县| 达州市| 田东县| 南丹县| 广安市| 西华县| 松溪县| 宝清县| 河曲县| 阿尔山市| 宁武县| 丰台区| 西乡县| 贞丰县| 巧家县| 丹江口市| 塔河县| 唐山市| 五华县| 扬州市| 祥云县| 静安区| 兴文县| 景东| 中超| 洪江市| 尚义县| 肥东县| 和平县| 千阳县| 东海县| 宁陵县| 黄龙县| 关岭|