大家好,我是小小明,今天我要給大家分享一個用python實現(xiàn)的仿Linux的tree命令。
詳見:https://pypi.org/project/filestools/
通過以下命令安裝即可直接使用:
pip install filestools -U
安裝后的使用示例:
對如何編碼實現(xiàn)感興趣的童鞋可以繼續(xù)往下看。
在CentOS的Linux系統(tǒng)下,我們可以再使用yum命令安裝tree之后使用tree。安裝命令:
yum install tree
然后使用tree命令的樹形顯示效果:
[root@iZwz9afmmytm54pshbwmebZ 018]# tree
.
├── css
│ ├── abstract\ blue\ lights\ orange\ bokeh\ gaussian\ blur\ 1920x1200\ wallpaper_www.wallpaperhi.com_43.jpg
│ ├── default.css
│ ├── font
│ │ ├── DS-DIGIB.TTF
│ │ ├── DS-DIGII.TTF
│ │ ├── DS-DIGI.TTF
│ │ └── DS-DIGIT.TTF
│ ├── jquery-ui.css
│ ├── mobile.css
│ ├── normalize.css
│ └── style.css
├── images
│ ├── btn01slider2.png
│ ├── charts.png
│ ├── logofont.png
│ ├── logoline1.png
│ ├── logoline2.png
│ ├── logoline3.png
│ └── logoline.png
├── index.html
├── js
│ ├── common.js
│ ├── index.js
│ ├── jquery-1.8.3.min.js
│ └── jquery_and_jqueryui.js
└── less
└── style.less
加上-C參數(shù)對各種類型加上不同的顏色:
加上-s參數(shù)能額外列出文件或目錄的大小,-h參數(shù)用于自動修正顯示單位:
tree命令參數(shù)說明:
Windows上也有tree命令,只不過沒有Linux平臺的tree命令強大。
我們看看顯示效果:
D:\QMDownload\source\test>tree /F
文件夾 PATH 列表
卷序列號為 5A3F-F8A8
D:.
│ index.html
│
├─css
│ │ abstract blue lights orange bokeh gaussian blur 1920x1200 wallpaper_www.wallpaperhi.com_43.jpg
│ │ default.css
│ │ jquery-ui.css
│ │ mobile.css
│ │ normalize.css
│ │ style.css
│ │
│ └─font
│ DS-DIGI.TTF
│ DS-DIGIB.TTF
│ DS-DIGII.TTF
│ DS-DIGIT.TTF
│
├─images
│ btn01slider2.png
│ charts.png
│ logofont.png
│ logoline.png
│ logoline1.png
│ logoline2.png
│ logoline3.png
│
├─js
│ common.js
│ index.js
│ jquery-1.8.3.min.js
│ jquery_and_jqueryui.js
│
└─less
style.less
個人只能說勉強還行吧,也能看清楚樹形結(jié)構(gòu)。
下面呢,我們考慮使用Python來仿制這樣的命令。
關(guān)于Python打印樹形目錄結(jié)構(gòu),我已經(jīng)在4年前使用Java寫過一個不夠完善的代碼。19年國慶學python的時候才用os模塊重寫了一下:
import os
def show_dir(path, layer=0):
listdir = os.listdir(path)
for i, file in enumerate(listdir, 1):
file_path = os.path.join(path, file)
print("│ " * (layer - 1), end="")
if (layer > 0):
print("└─" if i == len(listdir) else "├─", end="")
print(file)
if (os.path.isdir(file_path)):
show_dir(file_path, layer + 1)
show_dir("test")
結(jié)果:
css
├─abstract blue lights orange bokeh gaussian blur 1920x1200 wallpaper_www.wallpaperhi.com_43.jpg
├─default.css
├─font
│ ├─DS-DIGI.TTF
│ ├─DS-DIGIB.TTF
│ ├─DS-DIGII.TTF
│ └─DS-DIGIT.TTF
├─jquery-ui.css
├─mobile.css
├─normalize.css
└─style.css
images
├─btn01slider2.png
├─charts.png
├─logofont.png
├─logoline.png
├─logoline1.png
├─logoline2.png
└─logoline3.png
index.html
js
├─common.js
├─index.js
├─jquery-1.8.3.min.js
└─jquery_and_jqueryui.js
less
└─style.less
還行,但是對于很深的目錄缺點也很明顯,例如出現(xiàn)這樣的顯示情況:
要自行完全實現(xiàn)Linux的樹形目錄比較復雜,所幸python有個第三方庫rich
中的Tree模塊能支持彩色和樹形輸出。
安裝命令:
pip install rich
詳細使用方式可以參考官方文檔:https://rich.readthedocs.io/en/stable/
Tree模塊的使用示例:https://github.com/willmcgugan/rich/blob/master/examples/tree.py
這個官方的代碼示例就是專門用來樹形顯示目錄的,我們可以復制粘貼到jupyter中稍微改改玩一下。
上述代碼底部修改的部分:
directory = os.path.abspath("test")
tree = Tree(
f":open_file_folder: [link file://{directory}]{directory}",
guide_style="bold bright_blue",
)
walk_directory(pathlib.Path(directory), tree)
print(tree)
顯示效果比Linux的tree命令更秀。不過這個腳本兼容性較差,Windows控制臺并不支持顯示圖標之類的,導致會出現(xiàn)亂碼:
由于官方自帶案例秀過頭了兼容性不太好,所以我們自行編碼:
"""
小小明的代碼
CSDN主頁:https://blog.csdn.net/as604049322
"""
__author__ = '小小明'
import os
import sys
import rich
from rich.text import Text
from rich.tree import Tree
def get_file_size(file):
size = os.path.getsize(file)
if size == 0:
return "空文件"
num = 0
while size > 1024:
size /= 1024
num += 1
unit = ["", "KB", "MB", "GB", "TB"]
return f"{size:.2f}".rstrip(".0") + unit[num]
def show_dir(path, tree=None):
if tree is None:
tree = Tree(f"[bold magenta]{os.path.abspath(path)}")
for file in os.listdir(path):
file_path = os.path.join(path, file)
if (os.path.isdir(file_path)):
parent = tree.add(f"[bold magenta]{file}")
show_dir(file_path, parent)
else:
text_filename = Text(file, "green")
text_filename.highlight_regex(r"\.[^.]+$", "bold red")
text_filename.append(f" ({get_file_size(file_path)})", "bold blue")
tree.add(text_filename)
return tree
if __name__ == '__main__':
rich.print(show_dir(sys.argv[1]))
將以上代碼保存為tree.py
,然后在jupyter中執(zhí)行:
from tree import show_dir
import rich
rich.print(show_dir("test"))
在Windows控制臺中的執(zhí)行結(jié)果:
python tree.py test
將腳本上傳到Linux看下Linux下的執(zhí)行效果:
可以看到我們自行編寫的腳本已經(jīng)能夠同時適用于windows和Linux平臺。
這就是tree模塊核心邏輯的開發(fā),至此我們的目標就已經(jīng)達成。