python在日志文件中找到FAILED数字

时间:2016-09-01 19:54:44

标签: python windows robocopy

我希望Python代码从file.txt中搜索FAILED号码。就像这里5,我需要搜索FAILED号码,无论是0还是任何数字。然后我可以发送电子邮件失败次数。   我试过grep但它不起作用。

searchfile = open("file.txt", "r")
for line in searchfile:
    if "FAILED" in line: print line
searchfile.close()


             Total    Copied   Skipped  Mismatch    FAILED    Extras
 Dirs :      2575         0      2575         0         5         0
Files :      6039         0      6039         0         0         0
Bytes :   1.547 g         0   1.547 g         0         0         0
Times :   0:00:53   0:00:00                       0:00:00   0:00:53


    Ended : Tue Aug 30 04:32:48 2016

1 个答案:

答案 0 :(得分:0)

您可以找到FAILED列的位置,并查看感兴趣的行中该位置的值:

result = {}
col = 'FAILED'
col_index = None
row_names = {'Dirs', 'Files'}

for l in open('file.txt').readlines():
    if col_index is None:
        if col in l:
            col_index = l.find(col)
    else:
        l_split = l.split()
        if len(l_split) > 0 and l_split[0] in row_names:
            result[l_split[0]] = int(l[col_index:col_index+len(col)])
print(result)