Skip to main content
7 of 7
added 5 characters in body
ndpu
  • 22.5k
  • 6
  • 58
  • 72

You can also do it with simple generator expression:

for line in (l for f in (file1, file2) for l in f):
    # do something with line

with this method you can specify some condition in expression itself:

for line in (l for f in (file1, file2) for l in f if 'text' in l):
    # do something with line which contains 'text'

The example above is equivalent to this generator with loop:

def genlinewithtext(*files):
    for file in files:
        for line in file:
            if 'text' in line:
                yield line

for line in genlinewithtext(file1, file2):
    # do something with line which contains 'text'
ndpu
  • 22.5k
  • 6
  • 58
  • 72