import re
import glob
import os
list_of_files = glob.glob('10.123.130*.txt')
pattern = re.compile(r"^\S+ \S+ \S+ 205\d+.*$")
extract_on = False
extracts_eds_upe = []
for fileName in list_of_files:
with open(fileName, 'r') as myfile:
print(myfile)
#lines = myfile.readlines()
for line in myfile:
if pattern.search(line) :
extract_on = True
extracts_eds_upe.append((line.rstrip('\n')))
with open(os.path.join(r'D:\Python Project\DRP\UPE', os.path.basename(fileName)), 'w') as mytext:
for line in extracts_eds_upe :
mytext.write("undo ")
mytext.write(line)
mytext.write('\n')
---------Input file#1 ----------
#
FTP server-source -i LoopBack0
FTP client-source -i LoopBack0
#
info-center loghost source LoopBack0
mpls switch-l2vc 10.123.146.97 205148001 tunnel-policy TE between 10.123.130.1 205148003 tunnel-policy TE backup 10.123.130.2 205148003 tunnel-policy TE encapsulation vlan
mpls switch-l2vc 10.124.24.165 205495401 tunnel-policy TE between 10.123.130.1 205495403 tunnel-policy TE backup 10.123.130.2 205495403 tunnel-policy TE encapsulation vlan
mpls switch-l2vc 10.123.146.53 205145401 tunnel-policy TE between 10.123.130.1 205145403 tunnel-policy TE backup 10.123.130.2 205145403 tunnel-policy TE encapsulation vlan
mpls switch-l2vc 10.123.146.49 213145001 tunnel-policy TE between 10.123.130.1 213145003 tunnel-policy TE backup 10.123.130.2 213145003 tunnel-policy TE encapsulation vlan
#
---------Input file#2 ----------
#
FTP server-source -i LoopBack0
FTP client-source -i LoopBack0
#
info-center loghost source LoopBack0
mpls switch-l2vc 10.123.146.85 205148001 tunnel-policy TE between 10.123.130.1 205148003 tunnel-policy TE backup 10.123.130.2 205148003 tunnel-policy TE encapsulation vlan
mpls switch-l2vc 10.124.24.16 205495401 tunnel-policy TE between 10.123.130.1 205495403 tunnel-policy TE backup 10.123.130.2 205495403 tunnel-policy TE encapsulation vlan
mpls switch-l2vc 10.123.146.49 213145001 tunnel-policy TE between 10.123.130.1 213145003 tunnel-policy TE backup 10.123.130.2 213145003 tunnel-policy TE encapsulation vlan
#
---------Expect output file#1 ----------
mpls switch-l2vc 10.123.146.97 205148001 tunnel-policy TE between 10.123.130.1 205148003 tunnel-policy TE backup 10.123.130.2 205148003 tunnel-policy TE encapsulation vlan
mpls switch-l2vc 10.124.24.165 205495401 tunnel-policy TE between 10.123.130.1 205495403 tunnel-policy TE backup 10.123.130.2 205495403 tunnel-policy TE encapsulation vlan
mpls switch-l2vc 10.123.146.53 205145401 tunnel-policy TE between 10.123.130.1 205145403 tunnel-policy TE backup 10.123.130.2 205145403 tunnel-policy TE encapsulation vlan
---------Expect output file#2 ----------
mpls switch-l2vc 10.123.146.85 205148001 tunnel-policy TE between 10.123.130.1 205148003 tunnel-policy TE backup 10.123.130.2 205148003 tunnel-policy TE encapsulation vlan
mpls switch-l2vc 10.124.24.16 205495401 tunnel-policy TE between 10.123.130.1 205495403 tunnel-policy TE backup 10.123.130.2 205495403 tunnel-policy TE encapsulation vlan
I have multiple text file and search pattern in file then write to the new file.But now the result in output multiple text are same , Actually should be not same . Now the problem is when read text file and match pattern that target then write to new file then read to second file and match the pattern then write to file also , but when write to second file have patrern from second file append from first I don't want like this I want to separate result pattern in each file output,anybody can help me please.
I just slightly modified your code, please look, it should work.
import re
import glob
import os
list_of_files = glob.glob('10.123.130*.txt')
pattern = re.compile(r"^\S+ \S+ \S+ 205\d+.*$")
for fileName in list_of_files:
with open(fileName, 'r') as myfile:
print(myfile)
# Moved inside the loop.
extract_on = False
extracts_eds_upe = []
for line in myfile:
if pattern.search(line) :
extract_on = True
extracts_eds_upe.append((line.rstrip('\n')))
# Moved inside the loop.
with open(os.path.join(r'D:\Python Project\DRP\UPE', os.path.basename(fileName)), 'w') as mytext:
for line in extracts_eds_upe :
mytext.write("undo ")
mytext.write(line)
mytext.write('\n')