欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

  • 簡(jiǎn)單幾步教你學(xué)會(huì)Python接口自動(dòng)化測(cè)試

    簡(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)用誕生 使用模板引擎和表單插件(2)

    一個(gè)基于flask的web應(yīng)用誕生第二篇,這篇文章主要介紹了如何使用jinja2模板引擎和wtf表單插件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • Django如何利用uwsgi和nginx修改代碼自動(dòng)重啟

    Django如何利用uwsgi和nginx修改代碼自動(dòng)重啟

    這篇文章主要介紹了Django如何利用uwsgi和nginx修改代碼自動(dòng)重啟問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • 詳解Python讀取yaml文件多層菜單

    詳解Python讀取yaml文件多層菜單

    這篇文章主要介紹了Python讀取yaml文件多層菜單,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • python-字典dict和集合set

    python-字典dict和集合set

    這篇文章主要介紹了python-字典dict和集合set,字典是python中的一種數(shù)據(jù)結(jié)構(gòu)。集合(set)與字典相同均存儲(chǔ)key,但只存儲(chǔ)key,key不可重復(fù),所以set中的值不可重復(fù),而且是無序,下面來看看更多相關(guān)內(nèi)容吧
    2021-12-12
  • Pygame Rect區(qū)域位置的使用(圖文)

    Pygame Rect區(qū)域位置的使用(圖文)

    在 Pygame 中我們使用 Rect() 方法來創(chuàng)建一個(gè)指定位置,大小的矩形區(qū)域。本文主要就來介紹一下如何使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2021-11-11
  • python?Pangu自動(dòng)美化中文排版工具使用探索

    python?Pangu自動(dòng)美化中文排版工具使用探索

    這篇文章主要為大家介紹了python?Pangu自動(dòng)美化中文排版工具使用探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01
  • 如何在Python?中使用?join()?函數(shù)把列表拼接成一個(gè)字符串

    如何在Python?中使用?join()?函數(shù)把列表拼接成一個(gè)字符串

    這篇文章主要介紹了如何在Python?中使用?join()?函數(shù)把列表拼接成一個(gè)字符串,文章圍繞?join()?函數(shù)的相關(guān)資料展開詳細(xì)內(nèi)容,需要的小伙伴可以參考一下,希望對(duì)你有幫助
    2022-03-03
  • Keras模型轉(zhuǎn)成tensorflow的.pb操作

    Keras模型轉(zhuǎn)成tensorflow的.pb操作

    這篇文章主要介紹了Keras模型轉(zhuǎn)成tensorflow的.pb操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • 解決Pandas生成Excel時(shí)的sheet問題的方法總結(jié)

    解決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

最新評(píng)論