Monday, 15 August 2011

python - How do you perform replacement of text in files and paste the replaced text at a specific position in one file -



python - How do you perform replacement of text in files and paste the replaced text at a specific position in one file -

""" using programme perform replamcement of text in 4 different files , compare tepmplate , paste replaced text @ specific position in single file.create new file 4 different files."""

#files directory contains 4 text file first file old text , has been # replaced first file new text , compared template , # pasted @ specific postion in single file. # other files.the input files , template remain unaltered.""" #https://docs.python.org/2/library/re.html import os,shutil,re directory='f:\\examples\\read_files\\files' one_step_up='f:\\examples\\read_files' os.chdir(directory) files=os.listdir(directory) ############################################################## shutil.copy(files[0],'floating_input_01.txt') shutil.copy(files[1],'floating_input_02.txt') shutil.copy(files[2],'floating_input_03.txt') shutil.copy(files[3],'floating_input_04.txt') ############################################################## lookup1= "#contents of first file#" lookup2= "#end of first file#" f1=open('floating_input_01.txt','r') open("floating_input_01.txt") myfile1: begin, line in enumerate(myfile1,1): if lookup1 in line: a1=begin f1.close() f2=open('floating_input_01.txt','r') open("floating_input_01.txt") myfile2: end, line in enumerate(myfile2,2): if lookup2 in line: b1=end f2.close() ############################################################## lookup1= "#contents of sec file#" lookup2= "#end of sec file#" f1=open('floating_input_02.txt','r') open("floating_input_02.txt") myfile: begin, line in enumerate(myfile,1): if lookup1 in line: a2=begin f1.close() f2=open('floating_input_02.txt','r') open("floating_input_02.txt") myfile: end, line in enumerate(myfile,1): if lookup2 in line: b2=end f2.close() ############################################################## lookup1= "#contents of 3rd file#" lookup2= "#end of 3rd file#" f1=open('floating_input_03.txt','r') open("floating_input_03.txt") myfile: begin, line in enumerate(myfile,1): if lookup1 in line: a3=begin f1.close() f2=open('floating_input_03.txt','r') open("floating_input_03.txt") myfile: end, line in enumerate(myfile,1): if lookup2 in line: b3=end f2.close() ############################################################## lookup1= "#contents of 4th file#" lookup2= "#end of 4th file#" f1=open('floating_input_04.txt','r') open("floating_input_04.txt") myfile: begin, line in enumerate(myfile,1): if lookup1 in line: a4=begin f1.close() f2=open('floating_input_04.txt','r') open("floating_input_04.txt") myfile: end, line in enumerate(myfile,1): if lookup2 in line: b4=end f2.close() ############################################################## f=open('floating_input_01.txt','r') fout=open('out_01.txt','w') open("floating_input_01.txt") myfile: lines=f.readlines() in range(a1,b1-2): fout.write(lines[i]) f.close() fout.close() ############################################################## f=open('floating_input_02.txt','r') fout=open('out_02.txt','w') open("floating_input_02.txt") myfile: lines=f.readlines() in range(a2,b2-1): fout.write(lines[i]) f.close() fout.close() ############################################################## f=open('floating_input_03.txt','r') fout=open('out_03.txt','w') open("floating_input_03.txt") myfile: lines=f.readlines() in range(a3,b3-1): fout.write(lines[i]) f.close() fout.close() ############################################################## f=open('floating_input_04.txt','r') fout=open('out_04.txt','w') open("floating_input_04.txt") myfile: lines=f.readlines() in range(a4,b4-1): fout.write(lines[i]) f.close() fout.close() ############################################################## f=open('out_01.txt','r+') stra1=re.sub(r'first file old text',"first file new text",f.read(),re.m) f.close() f=open('out_02.txt','r+') stra2=re.sub(r'second file old text',"second file new text",f.read(),re.m) f.close() f=open('out_03.txt','r+') stra3=re.sub(r'third file old text',"third file new text",f.read(),re.m) f.close() f=open('out_04.txt','r+') stra4=re.sub(r'fourth file old text',"fourth file new text",f.read(),re.m) f.close() ################################################################ os.chdir(one_step_up) files=os.listdir(one_step_up) shutil.copy('template.txt','joint.txt') open('template.txt') fin, open('joint.txt','r+') fout: line in fin: fout.write(line) if line == "paste first file here\n": fout.write(stra1) if line == "paste sec file here\n": fout.write(stra2) if line == "paste 3rd file here\n": fout.write(stra3) if line == "paste 4th file here\n": fout.write(stra4) ################################################################ os.chdir(directory) os.remove('out_01.txt') os.remove('out_02.txt') os.remove('out_03.txt') os.remove('out_04.txt') os.remove('floating_input_01.txt') os.remove('floating_input_02.txt') os.remove('floating_input_03.txt') os.remove('floating_input_04.txt') #################################################################

import os,shutil,re directory='f:\\examples\\read_files\\files' one_step_up='f:\\examples\\read_files' os.chdir(directory) files=os.listdir(directory) ############################################################## shutil.copy(files[0],'floating_input_01.txt') shutil.copy(files[1],'floating_input_02.txt') shutil.copy(files[2],'floating_input_03.txt') shutil.copy(files[3],'floating_input_04.txt') ############################################################## lookup1= "#contents of first file#" lookup2= "#end of first file#" f1=open('floating_input_01.txt','r') open("floating_input_01.txt") myfile1: begin, line in enumerate(myfile1,1): if lookup1 in line: a1=begin f1.close() f2=open('floating_input_01.txt','r') open("floating_input_01.txt") myfile2: end, line in enumerate(myfile2,2): if lookup2 in line: b1=end f2.close() ############################################################## lookup1= "#contents of sec file#" lookup2= "#end of sec file#" f1=open('floating_input_02.txt','r') open("floating_input_02.txt") myfile: begin, line in enumerate(myfile,1): if lookup1 in line: a2=begin f1.close() f2=open('floating_input_02.txt','r') open("floating_input_02.txt") myfile: end, line in enumerate(myfile,1): if lookup2 in line: b2=end f2.close() ############################################################## lookup1= "#contents of 3rd file#" lookup2= "#end of 3rd file#" f1=open('floating_input_03.txt','r') open("floating_input_03.txt") myfile: begin, line in enumerate(myfile,1): if lookup1 in line: a3=begin f1.close() f2=open('floating_input_03.txt','r') open("floating_input_03.txt") myfile: end, line in enumerate(myfile,1): if lookup2 in line: b3=end f2.close() ############################################################## lookup1= "#contents of 4th file#" lookup2= "#end of 4th file#" f1=open('floating_input_04.txt','r') open("floating_input_04.txt") myfile: begin, line in enumerate(myfile,1): if lookup1 in line: a4=begin f1.close() f2=open('floating_input_04.txt','r') open("floating_input_04.txt") myfile: end, line in enumerate(myfile,1): if lookup2 in line: b4=end f2.close() ############################################################## f=open('floating_input_01.txt','r') fout=open('out_01.txt','w') open("floating_input_01.txt") myfile: lines=f.readlines() in range(a1,b1-2): fout.write(lines[i]) f.close() fout.close() ############################################################## f=open('floating_input_02.txt','r') fout=open('out_02.txt','w') open("floating_input_02.txt") myfile: lines=f.readlines() in range(a2,b2-1): fout.write(lines[i]) f.close() fout.close() ############################################################## f=open('floating_input_03.txt','r') fout=open('out_03.txt','w') open("floating_input_03.txt") myfile: lines=f.readlines() in range(a3,b3-1): fout.write(lines[i]) f.close() fout.close() ############################################################## f=open('floating_input_04.txt','r') fout=open('out_04.txt','w') open("floating_input_04.txt") myfile: lines=f.readlines() in range(a4,b4-1): fout.write(lines[i]) f.close() fout.close() ############################################################## f=open('out_01.txt','r+') stra1=re.sub(r'first file old text',"first file new text",f.read(),re.m) f.close() f=open('out_02.txt','r+') stra2=re.sub(r'second file old text',"second file new text",f.read(),re.m) f.close() f=open('out_03.txt','r+') stra3=re.sub(r'third file old text',"third file new text",f.read(),re.m) f.close() f=open('out_04.txt','r+') stra4=re.sub(r'fourth file old text',"fourth file new text",f.read(),re.m) f.close() ################################################################ os.chdir(one_step_up) files=os.listdir(one_step_up) shutil.copy('template.txt','joint.txt') open('template.txt') fin, open('joint.txt','r+') fout: line in fin: fout.write(line) if line == "paste first file here\n": fout.write(stra1) if line == "paste sec file here\n": fout.write(stra2) if line == "paste 3rd file here\n": fout.write(stra3) if line == "paste 4th file here\n": fout.write(stra4) ################################################################ os.chdir(directory) os.remove('out_01.txt') os.remove('out_02.txt') os.remove('out_03.txt') os.remove('out_04.txt') os.remove('floating_input_01.txt') os.remove('floating_input_02.txt') os.remove('floating_input_03.txt') os.remove('floating_input_04.txt') #################################################################

python regex shutil

No comments:

Post a Comment