溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用Python代碼實(shí)現(xiàn)Linux中的ls遍歷目錄命令的實(shí)例代碼

發(fā)布時間:2020-09-26 07:00:26 來源:腳本之家 閱讀:196 作者:TM0831 欄目:開發(fā)技術(shù)

一、寫在前面

  前幾天在微信上看到這樣一篇文章,鏈接為:https://www.jb51.net/it/692145.html,在這篇文章中,有這樣一段話,吸引了我的注意:

  使用Python代碼實(shí)現(xiàn)Linux中的ls遍歷目錄命令的實(shí)例代碼

  在 Linux 中 ls 是一個使用頻率非常高的命令了,可選的參數(shù)也有很多, 算是一條不得不掌握的命令。Python 作為一門簡單易學(xué)的語言,被很多人認(rèn)為是不需要認(rèn)真學(xué)的,或者只是隨便調(diào)個庫就行了,那可就真是小瞧 Python 了。那這次我就要試著用 Python 來實(shí)現(xiàn)一下 Linux 中的 ls 命令, 小小地證明下 Python 的不簡單!

二、ls簡介

  Linux ls 命令用于顯示指定工作目錄下的內(nèi)容。語法如下:

ls [-alkrt] [name]

  這里只列舉了幾個常用的參數(shù),ls 命令的可選參數(shù)還是很多的,可以使用 man ls 來進(jìn)行查看具體信息。這里列出的幾個參數(shù)對應(yīng)含義如下:

  1)-a:顯示所有文件及目錄;

  2)-l:除文件名稱外,亦將文件大小、創(chuàng)建時間等信息列出;

  3)-k:將文件大小以 KB 形式表示;

  4)-r:將文件以相反次序排列;

  5)-t:將文件以修改時間次序排列。

三、具體思路

  主要使用的模塊是 argparse 和 os,其中 argparse 模塊能設(shè)置和接收命令行參數(shù),也就使得 Python 對命令行的操作變得簡單,而 os 模塊則用于文件操作,對 argparse 模塊不熟悉的可以在這里查看官方文檔。

  既然要用 Python 實(shí)現(xiàn) ls.py, 也就要在命令行中進(jìn)行操作,比如 python ls.py -a 這樣的命令,而對 Python 比較熟悉的人可能會想到使用 sys 模塊來接收輸入的命令,但使用 argparse 能讓命令行操作變得更加簡單!首先要導(dǎo)入模塊并創(chuàng)建一個 ArgumentParser 對象,可以理解為一個解析器,然后就可以通過使用 add_argument() 方法為這個解析器添加參數(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))

這段代碼的功能是輸入一到多個整數(shù),默認(rèn)求其中的最大值,若有 -min 參數(shù)則是求其中的最小值??梢钥吹皆趧?chuàng)建解析器和添加命令行參數(shù)的時候都設(shè)置了 description 描述信息,這個信息會在我們使用 --help 命令的時候顯示出來,例如:

使用Python代碼實(shí)現(xiàn)Linux中的ls遍歷目錄命令的實(shí)例代碼

在上面的代碼中,需要注意的是其中使用 add_argument() 添加了一個位置參數(shù) "integers" 和一個可選參數(shù) "-min",位置參數(shù)在命令行中必須存在,不可遺漏,也就不能設(shè)置 required 參數(shù)了,而可選參數(shù)就不是必須要有的了,因而還可以使用 default 參數(shù)設(shè)置默認(rèn)值。nargs 參數(shù)用于設(shè)置命令行參數(shù)的數(shù)量,"+" 表示一個或多個,"?" 表示零個或一個,這里由于輸入的數(shù)字可能有多個,所以要設(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 的介紹就到此為止了,下面簡單介紹下 os 模塊, os 模塊提供了便捷的使用操作系統(tǒng)相關(guān)功能的方式,實(shí)現(xiàn) ls.py 所用到的該模塊下的方法包括:

  1)os.path.isdir(path):若 path 是一個存在的目錄,返回 True。

  2)os.listdir(path):返回一個列表,其中包括 path 對應(yīng)的目錄下的內(nèi)容,不包含“.”和“..”,即使它們存在。

  3)os.stat(path):獲取文件或文件描述符的狀態(tài),返回一個 stat_result 對象,其中包含了各種狀態(tài)信息。

四、主要代碼

  ls.py 中的主函數(shù)如下,主要功能為創(chuàng)建解析器,設(shè)置可選參數(shù)和位置參數(shù),然后接收命令行參數(shù)信息,并根據(jù)輸入的參數(shù)調(diào)用相應(yīng)的方法,這里設(shè)置了一個 "-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)

然后是一個獲取指定路徑下的內(nèi)容信息的函數(shù),要做的就是判斷路徑是否存在,若存在就返回一個文件列表,若不存在則顯示錯誤信息,并退出程序。

def get_all(path):
  """
  獲取指定路徑下的全部內(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,這里并沒有輸入路徑,就會使用默認(rèn)路徑即當(dāng)前目錄,如下圖:

使用Python代碼實(shí)現(xiàn)Linux中的ls遍歷目錄命令的實(shí)例代碼

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

  使用Python代碼實(shí)現(xiàn)Linux中的ls遍歷目錄命令的實(shí)例代碼

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

使用Python代碼實(shí)現(xiàn)Linux中的ls遍歷目錄命令的實(shí)例代碼

到這里為止,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):
 """
 獲取指定路徑下的全部內(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"]:
    # 格式化時間,文件大小用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:
    # 格式化時間,文件大小用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對象
 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!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI