我有一個文件夾,其中包含我將用python解析的幾個日誌文件。Python從列表中選擇一個文件
我將顯示包含到像一個文件夾中的文件列表:
- [1] FileName1.log
- [2] FileName2.log
,然後用戶可以選擇正確的文件寫入文件列表號碼。
例如,解析文件「FileName2.log」用戶請按2
在我的劇本我可以顯示文件的列表,但我現在不如何從列表中選擇一個文件按指數。
這是我的腳本
import os
import sys
items = os.listdir("D:/Logs")
fileList = []
for names in items:
if names.endswith(".log"):
fileList.append(names)
cnt = 0
for fileName in fileList:
sys.stdout.write("[%d] %s\n\r" %(cnt, fileName))
cnt = cnt + 1
fileName = raw_input("\n\rSelect log file [0 -" + str(cnt) + " ]: ")
感謝您的幫助!
的Python的版本是您使用解決您的目的是什麼? – inspectorG4dget
提示:使用枚舉有一個更pythonic和可讀的代碼'cnt,fileName枚舉(fileList):' –