I am trying to open a file and search for a particular string in a line and replace it with another string.
I am trying the following code.
def myFunct(file, test, patter, replace):
with open(file, mode='r') as f:
for line in f.readline():
if str(line).__contains__(test):
if patter in line:
print("Found here\n")
print(line)
f.close()
The code does not seem to go into the for loop. Any suggestions ?
I have also tried a similar solution with the same problem.
You are only reading the first line and iterate over the single characters of that line. You have to remove the readline
:
def myFunct(file, test, patter, replace):
with open(file, mode='r') as f:
for line in f:
if test in line and patter in line:
print("Found here\n")
print(line)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.