日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

乌拉、利用python实现tree命令

發布時間:2023/12/31 python 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 乌拉、利用python实现tree命令 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

由于服務器中沒有tree命令,且自己沒有權限install新命令,只能自個用python實現該命令,費勁

具體實現方法如下

以下操作基于Linux,python2

1. 首先就是隨便選個位置,然后新建個py文件(這里選在了"~/.basic_scipt",命名為"tree.py",可參考)。py文件內容如下:

#!/usr/bin/python2 # coding:utf-8 import os import sys# -d 只顯示目錄 # -a 顯示所有目錄 # -L num 顯示幾層,不顯示隱藏文件def print_color(color,argv):if color == "green":print("\033[32m"+argv+"\033[0m")elif color == "read":print("\033[31m"+argv+"\033[0m")elif color == "yellow":print("\033[33m"+argv+"\033[0m")else :print(argv)def is_hidden(file):file_name = os.path.basename(file)if file_name[0] == '.':return Trueelse:return Falsedef get_len_of_listdir(lst,show_hidden=True):len_of_listdir = 0for file in lst:if show_hidden == True:len_of_listdir += 1elif is_hidden(file) is not True:len_of_listdir += 1return len_of_listdirdef tree_dir(dir, show_hidden_file, direct_only, layer_limit, layer_max,layer=0):layer_max = int(layer_max)files = os.listdir(dir)files.sort()file_lst=[]dir_lst=[]for file in files:file_path = os.path.join(dir, file)if os.path.isdir(file_path):dir_lst.append(file)else:file_lst.append(file)if show_hidden_file == False:len_of_listdir = get_len_of_listdir(dir_lst)len_of_listfile = get_len_of_listdir(file_lst)else:len_of_listdir = get_len_of_listdir(dir_lst,show_hidden=False)len_of_listfile = get_len_of_listdir(file_lst,show_hidden=False)if direct_only is not True:index = 0for file in file_lst:file_path = os.path.join(dir, file) if layer_limit == True and layer >= layer_max:continueif show_hidden_file == False and is_hidden(file) is True:continue index += 1print(" | " * (layer)),if (layer >= 0):if index == len_of_listfile and len_of_listdir == 0:print("|__ "),else:print("|__ "),print(file)index = 0for file in dir_lst:file_path = os.path.join(dir, file) if layer_limit == True and layer >= layer_max:continueif show_hidden_file == False and is_hidden(file) is True:continue index += 1print(" | " * (layer)),if (layer >= 0):if index == len_of_listdir:print("|__ "),else:print("|__ "),print_color("green", file)tree_dir(file_path, show_hidden_file, direct_only, layer_limit, layer_max,layer + 1)def parse_option(op_str):print("option:" + op_str)import relayer_limit = Falselayer_max = 0direct_only = Falseshow_hidden_file = Falsematch_str = format("-L[\s]+\d")search_ret = re.search(match_str, op_str)if search_ret is not None:layer_limit = Truematch_str = format("\d")search_ret = re.search(match_str, search_ret.group(0))if search_ret is not None:layer_max = search_ret.group(0)match_str = format("-a")search_ret = re.search(match_str, op_str)if search_ret is not None:show_hidden_file = Truematch_str = format("-d")search_ret = re.search(match_str, op_str)if search_ret is not None:direct_only = Truereturn show_hidden_file, direct_only, layer_limit, layer_maxif __name__ == '__main__':file_name = sys.argv[0]if len(sys.argv) < 2:print_color("red", "args invalid\n")sys.exit(1)path_dir = sys.argv[1]op = ""for idx in range(len(sys.argv)):if idx >= 2:op += " " + sys.argv[idx]show_hidden_file, direct_only, layer_limit, layer_max = parse_option(op)tree_dir(path_dir,show_hidden_file, direct_only, layer_limit, layer_max)sys.exit(0)

2. 功能選項如下所示,詳見第五條內容中的代碼部分

# -d 只顯示目錄 # -a 顯示所有目錄 # -L num 顯示幾層,不顯示隱藏文件

3. 找到home目錄下的".bashrc"文件(或".cshrc"文件,均為隱藏文件。這里用的是bashrc文件)。進入文件后編輯,隨意找個位置添加一行alias指令,內容如下:

alias tree='python ~/.basic_scipt/tree.py ./'

如果沒有找到".bashrc"文件,修改".cshrc"文件亦可,命令如下(bashrc和cshrc修改一個即可):

alias tree 'python ~/.basic_scipt/tree.py ./' #注意沒有等號=

4.完成以上操作之后,記得保存并退出。然后在命令終端執行如下指令(source),以使bashrc文件(或cshrc文件)的更改生效:

$ source ~/.bashrc # 或 $ source ~/.cshrc

5. 在source命令執行完之后,就已經大功告成了,趕緊找個目錄tree一下試試吧。如果報錯的話,就看看python代碼部分有沒有錯誤吧。

$ tree #沒錯就這么簡單,但還可以加一些參數,如下 $ tree -L 2 #顯示兩層文件,避免眼花繚亂 $ tree -d #只顯示目錄

6. 需要注意的是,該代碼是使用的python2平臺,如是python3平臺的話,估計 肯定會出現不兼容,然后報錯。

7. 需要注意的第二點,由于對該命令作了些簡化,所以只能實現對當前所在路徑的tree,即加一些路徑當作參數是不可以的。例:

$ tree ../ #這是錯誤的用法

python代碼參考了其他人的博客,略微做了些修改,忘記出處了,見諒

如果您在實際使用中出現問題或者有更好的解決辦法,歡迎留言。

總結

以上是生活随笔為你收集整理的乌拉、利用python实现tree命令的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。