1. 程式人生 > >查詢指定資料夾下、指定字尾名的檔案

查詢指定資料夾下、指定字尾名的檔案

描述:找出資料夾“C:\Users\12556\Desktop\all-test-jobs\all-yumh-err\“下字尾名為.txt的所有檔案,每行顯示一個

命令dir C:\Users\12556\Desktop\all-test-jobs\all-yumh-err\*.txt /b

引數說明:

/P:當顯示的資訊超過一屏時暫停顯示,直至按任意鍵才繼續顯示

/W:以橫向排列的形式顯示檔名和目錄名,每行5個(不顯示檔案大小、建立日期和時間)

/S:列出指定目錄及其子目錄中的所有內容(但不包括系統檔案和隱含檔案)

/B:僅列出檔名稱,而不列出日期、大小等資訊

/A:顯示包括系統檔案和隱含檔案

python程式碼
執行語句(只有一個引數,是日誌資料夾的路徑。tips: sys.argv.len() ==2):

"D:\Special Program\Python27\python.exe" C:/Users/12556/PycharmProjects/sls/getJobKeyTime.py C:\Users\12556\Desktop\all-test-jobs\all-yumh-err\

呼叫函式getFileList(yumhErrDir,".txt")

適用於ls和dir函式程式碼:

isWindows = True

def getFileList(yumhErrDir, suffix)
:
if(isWindows): fileList = os.popen("dir " + yumhErrDir + "*" + suffix + " /B") else: os.chdir(yumhErrDir) fileList = os.popen("ls *.txt").readlines() for logFile in fileList: logFile = logFile.strip('\n') print logFile

輸出結果的一部分:
這裡寫圖片描述

注意:如果想統計檔案個數,應該定義一個變數統計,而不能用fileList.sizeof

()