set row number to document and get data in python

Viewed 54

I have a series of files and I have to separate and display part of the text

my code is :

path = 'C:\\Bot\\*.log' 
files = glob.glob(path) 
nlines = 0
for name in files: 
    try: 
        with open(name) as f:
            for line in f :
                 nlines += 1
                 if (line.find("Total") >= 0):
                     print(line)
                     

I need a text that is saved in the file after the line number is obtained. With the above code, I have access to the line number but I do not have access to some subsequent lines
How to access the next line value??

2 Answers
path = 'C:\\Bot\\*.log' 
files = glob.glob(path) 
nlines = 0
for name in files: 
    try: 
        with open(name) as f:
            for line in f:
                 nlines += 1
                 if (line.find("Total") >= 0):
                      print(next(f))

I think it is a better solution to this problem

Use next() to read:

path = 'C:\\Bot\\*.log' 
files = glob.glob(path) 
nlines = 0
for name in files: 
    try: 
        with open(name) as f:
            for line in f:
                 nlines += 1
                 if (line.find("Total") >= 0):
                      for i in range(6):
                          print(next(f))
                     
Related