-4

I have text file. i want to open that text file and search one word called "OPR" if that word occured in the Final.txt file write 3 line of that particular string example code given below

text1="Some decoders allow you to manipulate the image reading"
text2="file.This can often be used to speed up decoding when creating" 
text3="when speed is usually more important than quality  printing"
text4="monochrome laser printer when only a greyscale"
text4="Note that the resulting image may not exactly match the requested"
text5="mode and size. To make sure that the image is not larger"
text6="given size, use the thumbnail method instead."


output_file=open("Final.txt","a")
output_file.write(text1)
output_file.write(text2)
output_file.write(text3)
output_file.write(text4)
output_file.write(text5)
output_file.write(text6)
output_file.close()

import collections
import itertools
import sys
with open('output\\Final.txt') as f:
  for line in f:
    if 'used' in line:
        print("OPR")
        sys.stdout.write(line)
        sys.stdout.writelines(itertools.islice(f, 4))

my doubt is i have 3 text files called Finaltxt.1,Finaltxt.2,Finaltxt.3 in the some directory how i do check the "used" word for all three text files and write 3 lines of text which i have done above code

thewaywewere
  • 8,128
  • 11
  • 41
  • 46
Pyd
  • 6,017
  • 18
  • 52
  • 109

1 Answers1

0

Check out the code below, as I think it does what you want to accomplish:

output_file=open("file.txt","w")
output_file.write("file: lineNo1 \n")
output_file.write("file: lineNo2 used \n")
output_file.write("file: lineNo3 \n")
output_file.write("file: lineNo4 used \n")
output_file.write("file: lineNo5 \n")
output_file.write("file: lineNo6 \n")
output_file.close() 

output_file=open("file1.txt","w")
output_file.write("file1: lineNo1 \n")
output_file.write("file1: lineNo2 \n")
output_file.write("file1: lineNo3 \n")
output_file.write("file1: lineNo4 used \n")
output_file.write("file1: lineNo5 \n")
output_file.write("file1: lineNo6 \n")
output_file.close()

output_file=open("file2.txt","w")
output_file.write("file2: lineNo1 \n")
output_file.write("file2: lineNo2 \n")
output_file.write("file2: lineNo3 \n")
output_file.write("file2: lineNo4 \n")
output_file.write("file2: lineNo5 \n")
output_file.write("file2: lineNo6 used \n")
output_file.close()

import collections
import itertools
import sys
with open('file.txt') as f, open('file1.txt') as f1, open('file2.txt') as f2 :
  fTXT, f1TXT, f2TXT = f.readlines(), f1.readlines(), f2.readlines()   
  for lineNo in range(0, min(len(fTXT), len(f1TXT), len(f2TXT)) ):
      if 'used' in fTXT[lineNo] or 'used' in f1TXT[lineNo] or 'used' in f2TXT[lineNo]:
          print("OPR")
          sys.stdout.write("###" + fTXT[lineNo] + "###" + f1TXT[lineNo] + "###" + f2TXT[lineNo])

It outputs:

OPR
###file: lineNo2 used 
###file1: lineNo2 
###file2: lineNo2 
OPR
###file: lineNo4 used 
###file1: lineNo4 used 
###file2: lineNo4 
OPR
###file: lineNo6 
###file1: lineNo6 
###file2: lineNo6 used 
Claudio
  • 7,474
  • 3
  • 18
  • 48