w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
How do you perform replacement of text in files and paste the replaced text at a specific position in one file
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')
with open("FLOATING_INPUT_01.txt") as myFile1:
    for begin, line in enumerate(myFile1,1):
        if lookup1 in line:
            a1=begin            

f1.close()

f2=open('FLOATING_INPUT_01.txt','r')
with open("FLOATING_INPUT_01.txt") as myFile2:
    for end, line in enumerate(myFile2,2):
        if lookup2 in line:
            b1=end

f2.close()

##############################################################   

lookup1= "#CONTENTS OF SECOND FILE#"
lookup2= "#END OF SECOND FILE#"

f1=open('FLOATING_INPUT_02.txt','r')
with open("FLOATING_INPUT_02.txt") as myFile:
    for begin, line in enumerate(myFile,1):
        if lookup1 in line:
            a2=begin

f1.close()

f2=open('FLOATING_INPUT_02.txt','r')
with open("FLOATING_INPUT_02.txt") as myFile:
    for end, line in enumerate(myFile,1):
        if lookup2 in line:
            b2=end

f2.close()
##############################################################   

lookup1= "#CONTENTS OF THIRD FILE#"
lookup2= "#END OF THIRD FILE#"

f1=open('FLOATING_INPUT_03.txt','r')
with open("FLOATING_INPUT_03.txt") as myFile:
    for begin, line in enumerate(myFile,1):
        if lookup1 in line:
            a3=begin

f1.close()

f2=open('FLOATING_INPUT_03.txt','r')
with open("FLOATING_INPUT_03.txt") as myfile:
    for end, line in enumerate(myfile,1):
        if lookup2 in line:
            b3=end

f2.close()
##############################################################    

lookup1= "#CONTENTS OF FOURTH FILE#"
lookup2= "#END OF FOURTH FILE#"

f1=open('FLOATING_INPUT_04.txt','r')
with open("FLOATING_INPUT_04.txt") as myFile:
    for begin, line in enumerate(myFile,1):
        if lookup1 in line:
            a4=begin

f1.close()

f2=open('FLOATING_INPUT_04.txt','r')
with open("FLOATING_INPUT_04.txt") as myFile:
    for 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')

with open("FLOATING_INPUT_01.txt") as myFile:
    Lines=f.readlines()
    for i 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')

with open("FLOATING_INPUT_02.txt") as myFile:
    Lines=f.readlines()
    for i 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')

with open("FLOATING_INPUT_03.txt") as myFile:
    Lines=f.readlines()
    for i 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')

with open("FLOATING_INPUT_04.txt") as myFile:
    Lines=f.readlines()
    for i 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')
with open('TEMPLATE.txt') as fin, open('JOINT.txt','r+') as fout:
    for line in fin:
        fout.write(line)
        if line == "PASTE FIRST FILE HERE
":
           fout.write(stra1)
        if line == "PASTE SECOND FILE HERE
":
           fout.write(stra2)
        if line == "PASTE THIRD FILE HERE
":
           fout.write(stra3)
        if line == "PASTE FOURTH FILE HERE
":
           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')
#################################################################




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.