I am trying to search for a pattern in a text file and replace with another string.
I've opened the file in "r+" mode and used re.sub for replacement text.
The python output window is showing the desired result, but when I open the text file there is no change. The string that is to be replaced is not replaced in the text file.
Why is it so?
I've opened the file in "r+" mode and used re.sub for replacement text.
The python output window is showing the desired result, but when I open the text file there is no change. The string that is to be replaced is not replaced in the text file.
Why is it so?
Code:
# !/usr/bin/python
import os,re
fName = r'LOCATION OF FILE'
directory = os.path.dirname(fName)
os.chdir(directory)
fileName = os.path.basename(fName)
print(fileName)
o_File = open(fileName ,"r+")
with o_File as readFile:
for searchpattern in readFile:
found_Pattern = re.search("\s*\'[\d]+\==*[\d]*\.*[\d]*\==*[\d]*\.*[\d]*\'*",searchpattern,re.IGNORECASE)
if found_Pattern:
found_Pattern_Pat = found_Pattern.group()
print(value)
fValue = re.sub(r'==(.+)==','==100==',found_Pattern_Pat)
print(fValue)
o_File.close()
Comment