使用Python代碼實(shí)現(xiàn)Linux中的ls遍歷目錄命令的實(shí)例代碼
一、寫在前面
前幾天在微信上看到這樣一篇文章,鏈接為:http://www.dbjr.com.cn/it/692145.html,在這篇文章中,有這樣一段話,吸引了我的注意:

在 Linux 中 ls 是一個(gè)使用頻率非常高的命令了,可選的參數(shù)也有很多, 算是一條不得不掌握的命令。Python 作為一門簡(jiǎn)單易學(xué)的語(yǔ)言,被很多人認(rèn)為是不需要認(rèn)真學(xué)的,或者只是隨便調(diào)個(gè)庫(kù)就行了,那可就真是小瞧 Python 了。那這次我就要試著用 Python 來(lái)實(shí)現(xiàn)一下 Linux 中的 ls 命令, 小小地證明下 Python 的不簡(jiǎn)單!
二、ls簡(jiǎn)介
Linux ls 命令用于顯示指定工作目錄下的內(nèi)容。語(yǔ)法如下:
ls [-alkrt] [name]
這里只列舉了幾個(gè)常用的參數(shù),ls 命令的可選參數(shù)還是很多的,可以使用 man ls 來(lái)進(jìn)行查看具體信息。這里列出的幾個(gè)參數(shù)對(duì)應(yīng)含義如下:
1)-a:顯示所有文件及目錄;
2)-l:除文件名稱外,亦將文件大小、創(chuàng)建時(shí)間等信息列出;
3)-k:將文件大小以 KB 形式表示;
4)-r:將文件以相反次序排列;
5)-t:將文件以修改時(shí)間次序排列。
三、具體思路
主要使用的模塊是 argparse 和 os,其中 argparse 模塊能設(shè)置和接收命令行參數(shù),也就使得 Python 對(duì)命令行的操作變得簡(jiǎn)單,而 os 模塊則用于文件操作,對(duì) argparse 模塊不熟悉的可以在這里查看官方文檔。
既然要用 Python 實(shí)現(xiàn) ls.py, 也就要在命令行中進(jìn)行操作,比如 python ls.py -a 這樣的命令,而對(duì) Python 比較熟悉的人可能會(huì)想到使用 sys 模塊來(lái)接收輸入的命令,但使用 argparse 能讓命令行操作變得更加簡(jiǎn)單!首先要導(dǎo)入模塊并創(chuàng)建一個(gè) ArgumentParser 對(duì)象,可以理解為一個(gè)解析器,然后就可以通過(guò)使用 add_argument() 方法為這個(gè)解析器添加參數(shù)了。示例如下:
# test.py
import argparse
parser = argparse.ArgumentParser(description='Find the maximum number.')
parser.add_argument("integers", type=int, nargs="+", help="The input integers.")
parser.add_argument("-min", nargs="?", required=False, dest="find_num", default=max, const=min,
help="Find the minimum number(Default: find the maximum number).")
args = parser.parse_args()
print(args)
print(args.find_num(args.Nums))
這段代碼的功能是輸入一到多個(gè)整數(shù),默認(rèn)求其中的最大值,若有 -min 參數(shù)則是求其中的最小值??梢钥吹皆趧?chuàng)建解析器和添加命令行參數(shù)的時(shí)候都設(shè)置了 description 描述信息,這個(gè)信息會(huì)在我們使用 --help 命令的時(shí)候顯示出來(lái),例如:

在上面的代碼中,需要注意的是其中使用 add_argument() 添加了一個(gè)位置參數(shù) "integers" 和一個(gè)可選參數(shù) "-min",位置參數(shù)在命令行中必須存在,不可遺漏,也就不能設(shè)置 required 參數(shù)了,而可選參數(shù)就不是必須要有的了,因而還可以使用 default 參數(shù)設(shè)置默認(rèn)值。nargs 參數(shù)用于設(shè)置命令行參數(shù)的數(shù)量,"+" 表示一個(gè)或多個(gè),"?" 表示零個(gè)或一個(gè),這里由于輸入的數(shù)字可能有多個(gè),所以要設(shè)置為 "+"。最終運(yùn)行示例如下:
> python test.py 1 3 5
Namespace(find_num=<built-in function max>, integers=[1, 3, 5])
5> python test.py 1 3 5 -min
Namespace(find_num=<built-in function min>, integers=[1, 3, 5])
1
關(guān)于 argparse 的介紹就到此為止了,下面簡(jiǎn)單介紹下 os 模塊, os 模塊提供了便捷的使用操作系統(tǒng)相關(guān)功能的方式,實(shí)現(xiàn) ls.py 所用到的該模塊下的方法包括:
1)os.path.isdir(path):若 path 是一個(gè)存在的目錄,返回 True。
2)os.listdir(path):返回一個(gè)列表,其中包括 path 對(duì)應(yīng)的目錄下的內(nèi)容,不包含“.”和“..”,即使它們存在。
3)os.stat(path):獲取文件或文件描述符的狀態(tài),返回一個(gè) stat_result 對(duì)象,其中包含了各種狀態(tài)信息。
四、主要代碼
ls.py 中的主函數(shù)如下,主要功能為創(chuàng)建解析器,設(shè)置可選參數(shù)和位置參數(shù),然后接收命令行參數(shù)信息,并根據(jù)輸入的參數(shù)調(diào)用相應(yīng)的方法,這里設(shè)置了一個(gè) "-V" 參數(shù)用于顯示版本信息,可以使用 "-V" 或者 "-Version" 進(jìn)行查看。
def main():
"""
主函數(shù),設(shè)置和接收命令行參數(shù),并根據(jù)參數(shù)調(diào)用相應(yīng)方法
:return:
"""
# 創(chuàng)建解析器
parse = argparse.ArgumentParser(description="Python_ls")
# 可選參數(shù)
parse.add_argument("-a", "-all", help="Show all files", action="store_true", required=False)
parse.add_argument("-l", "-long", help="View in long format", action="store_true", required=False)
parse.add_argument("-k", help="Expressed in bytes", action="store_true", required=False)
parse.add_argument("-r", "-reverse", help="In reverse order", action="store_true", required=False)
parse.add_argument("-t", help="Sort by modified time", action="store_true", required=False)
parse.add_argument("-V", "-Version", help="Get the version", action="store_true", required=False)
# 位置參數(shù)
parse.add_argument("path", type=str, help="The path", nargs="?")
# 命令行參數(shù)信息
data = vars(parse.parse_args())
assert type(data) == dict
if data["V"]:
print("Python_ls version: 1.0")
return
else:
check_arg(data)
然后是一個(gè)獲取指定路徑下的內(nèi)容信息的函數(shù),要做的就是判斷路徑是否存在,若存在就返回一個(gè)文件列表,若不存在則顯示錯(cuò)誤信息,并退出程序。
def get_all(path):
"""
獲取指定路徑下的全部?jī)?nèi)容
:param path: 路徑
:return:
"""
if os.path.isdir(path):
files = [".", ".."] + os.listdir(path)
return files
else:
print("No such file or directory")
exit()
五、運(yùn)行結(jié)果
下面是 ls.py 運(yùn)行后的部分結(jié)果截圖。
首先是 python ls.py -a,這里并沒(méi)有輸入路徑,就會(huì)使用默認(rèn)路徑即當(dāng)前目錄,如下圖:

然后是 python ls.py -a -t .,使用該命令會(huì)顯示當(dāng)前目錄下的所有內(nèi)容,并按照創(chuàng)建的時(shí)間進(jìn)行排序,如下圖:

最后是 python ls.py -a -l -k -r .,也是顯示當(dāng)前目錄下的所有內(nèi)容并按照創(chuàng)建名稱排序,不過(guò)這次文件大小會(huì)以 KB 為單位來(lái)顯示,如下圖:

到這里為止,ls.py 就算是基本實(shí)現(xiàn)了,當(dāng)然還是有很多可以去實(shí)現(xiàn)的功能的,比如更多的參數(shù)等等,如果你感興趣的話可以自己嘗試一下==
完整python代碼
"""
Version: Python3.7
Author: OniOn
Site: http://www.cnblogs.com/TM0831/
Time: 2019/9/6 21:41
"""
import os
import time
import argparse
import terminaltables
def get(path):
"""
獲取指定路徑下的內(nèi)容
:param path: 路徑
:return:
"""
if os.path.isdir(path): # 判斷是否是真實(shí)路徑
files = os.listdir(path)
return files
else:
print("No such file or directory")
exit()
def get_all(path):
"""
獲取指定路徑下的全部?jī)?nèi)容
:param path: 路徑
:return:
"""
if os.path.isdir(path):
files = [".", ".."] + os.listdir(path)
return files
else:
print("No such file or directory")
exit()
def check_arg(data):
"""
檢查參數(shù)信息
:param data: 命令行參數(shù)(dict)
:return:
"""
assert type(data) == dict
if not data["path"]:
data["path"] = "."
# a參數(shù)
if data["a"]:
files = get_all(data["path"])
else:
files = get(data["path"])
# r參數(shù)
if data["r"]:
files = files[::-1]
# t參數(shù)
if data["t"]:
files = sorted(files, key=lambda x: os.stat(x).st_mtime)
for i in range(len(files)):
files[i] = [files[i], time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.stat(files[i]).st_mtime))]
# l參數(shù)
if data["l"]:
result = []
for i in range(len(files)):
file = files[i][0] if data["t"] else files[i]
# 獲取文件信息
file_info = os.stat(file)
# k參數(shù)
if data["k"]:
# 格式化時(shí)間,文件大小用KB表示
result.append([file, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(file_info.st_ctime)),
"%.3f KB" % (file_info.st_size / 1024)])
else:
# 格式化時(shí)間,文件大小用B表示
result.append([file, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(file_info.st_ctime)),
"{} Byte".format(file_info.st_size)])
if data["t"]:
for i in result:
i.append(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.stat(i[0]).st_mtime)))
show_file(result, True, True) if data["t"] else show_file(result, True, False)
return
show_file(files, False, True) if data["t"] else show_file(files, False, False)
def show_file(files, has_l, has_t):
"""
格式化輸出文件信息
:param files: 文件列表
:param has_l: 是否有l(wèi)參數(shù)
:param has_t: 是否有t參數(shù)
:return:
"""
# 根據(jù)參數(shù)信息設(shè)置表頭
if not has_l:
if not has_t:
table_data = [["ID", "FILE_NAME"]]
for i in range(len(files)):
table_data.append([i + 1, files[i]])
else:
table_data = [["ID", "FILE_NAME", "FILE_MTIME"]]
for i in range(len(files)):
table_data.append([i + 1] + files[i])
else:
if not has_t:
table_data = [["ID", "FILE_NAME", "FILE_CTIME", "FILE_SIZE"]]
else:
table_data = [["ID", "FILE_NAME", "FILE_CTIME", "FILE_SIZE", "FILE_MTIME"]]
for i in range(len(files)):
table_data.append([i + 1] + files[i])
# 創(chuàng)建AsciiTable對(duì)象
table = terminaltables.AsciiTable(table_data)
# 設(shè)置標(biāo)題
table.title = "file table"
for i in range(len(table.column_widths)):
if i != 1:
# 居中顯示
table.justify_columns[i] = "center"
print(table.table)
def main():
"""
主函數(shù),設(shè)置和接收命令行參數(shù),并根據(jù)參數(shù)調(diào)用相應(yīng)方法
:return:
"""
# 創(chuàng)建解析器
parse = argparse.ArgumentParser(description="Python_ls")
# 可選參數(shù)
parse.add_argument("-a", "-all", help="Show all files", action="store_true", required=False)
parse.add_argument("-l", "-long", help="View in long format", action="store_true", required=False)
parse.add_argument("-k", help="Expressed in bytes", action="store_true", required=False)
parse.add_argument("-r", "-reverse", help="In reverse order", action="store_true", required=False)
parse.add_argument("-t", help="Sort by modified time", action="store_true", required=False)
parse.add_argument("-V", "-Version", help="Get the version", action="store_true", required=False)
# 位置參數(shù)
parse.add_argument("path", type=str, help="The path", nargs="?")
# 命令行參數(shù)信息
data = vars(parse.parse_args())
assert type(data) == dict
if data["V"]:
print("Python_ls version: 1.0")
return
else:
check_arg(data)
if __name__ == '__main__':
main()
完整代碼已上傳到GitHub!
相關(guān)文章
Pytorch如何指定device(cuda or cpu)
這篇文章主要介紹了Pytorch如何指定device(cuda or cpu)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
Python中多線程的創(chuàng)建及基本調(diào)用方法
由于注明的GIL的存在,Python盡管能創(chuàng)建多個(gè)線程,但是多線程卻不能同時(shí)工作...well,這里我們來(lái)看一下Python中多線程的創(chuàng)建及基本調(diào)用方法2016-07-07
python使用suds調(diào)用webservice接口的方法
今天小編就為大家分享一篇python使用suds調(diào)用webservice接口的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
python-docx把dataframe表格添加到word文件中
用Python-docx庫(kù),可以輕松地添加表格到Word文檔中,本文主要介紹了python-docx把dataframe表格添加到word文件中,感興趣的可以了解一下2023-08-08
對(duì)Python實(shí)現(xiàn)累加函數(shù)的方法詳解
今天小編就為大家分享一篇對(duì)Python實(shí)現(xiàn)累加函數(shù)的方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
python scrapy框架中Request對(duì)象和Response對(duì)象的介紹
本文介紹了python基礎(chǔ)之scrapy框架中Request對(duì)象和Response對(duì)象的介紹,Request對(duì)象主要是用來(lái)請(qǐng)求數(shù)據(jù),爬取一頁(yè)的數(shù)據(jù)重新發(fā)送一個(gè)請(qǐng)求的時(shí)候調(diào)用,Response對(duì)象一般是由scrapy給你自動(dòng)構(gòu)建的,因此開(kāi)發(fā)者不需要關(guān)心如何創(chuàng)建Response對(duì)象,下面來(lái)一起來(lái)了解更多內(nèi)容吧2022-02-02

