I want to replace a string in a file created by my program, But I cant use .replace because It's not in 3.3, How do I replace a line from a file using two inputs(the previous string, replacement), Here Is the code so far:
#Data Creator
def Create(filename):
global UserFile
UserFile = open(str(filename), "w")
global file
file = (filename)
UserFile.close()
#Data Adder
def Add(data):
UserFile = open(file, "a")
UserFile.write(str(data))
UserFile.close()
#Data seeker
def Seek(target):
UserFile = open(file, "r")
UserFile.seek(target)
global postition
position = UserFile.tell(target)
UserFile.close()
return position
#Replace
def Replace(take,put):
UserFile = open(file, "r+")
UserFile.replace(take,put)
UserFile.close
Create("richardlovesdogs.txt")
Add("Richard loves all kinds of dogs including: \nbeagles")
Replace("beagles","pugs")
How do I do this, So that It replaces the word "beagles" with "pugs"? I'm learning python so any help would be appreciated
Edit :
ive changed the replace code to this
#Replace
def Replace(take,put):
UserFile = open(file, 'r+')
UserFileT = open(file, 'r+')
for line in UserFile:
UserFileT.write(line.replace(take,put))
UserFile.close()
UserFileT.close()
but in the file it outputs:
Richard loves all kinds of dogs including:
pugsles
how do i change it so it outputs only "pugs" not "pugsles"