Analyzing the text file using Python

advertisements

I have a huge log file which I want to parse using python. The file contains the data of working and non working audio/video/image file

The content of log.txt

filename = abc.mp4
played correctly

filname = cdf.wav
failed

filename = rtg.mp3
failed

Now from this log.txt how to find out failed filename I tried re.search but seems like I not reaching at any point with that approach So I used following approach but so far I am not able the way to find out the filename

f = open('log.txt', 'r')
for i in f.realines():
    if "failed" in i:
        print "failed files: "
f.close()


Save a reference to the filename temporarily, then add it to a list if it failed:

with open('log.txt') as f:
    result = []
    for line in f:
        line = line.strip()
        if not line:
            continue
        if '=' in line:
            name = line.split('= ')[1]
        if line == 'failed':
            result.append(name)

After the above, result will be a list with all the failed filenames.