0

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"

13

3 Answers 3

0

The first idea which came to my mind is to loop over lines and check if the given line contains the word which you want to replace. Then just use string method - replace. Of course, in the end the result should be put/written to the file.

Sign up to request clarification or add additional context in comments.

4 Comments

would this mean putting the entire file into a string then editing it? effectively overwriting the whole file?
you can use method readlines which create a list of lines for a given file, then you can loop over the list.
@mic4ael file.readlines also loads the whole file into memory, simply iterate over the file object to load one line at a time.
Take note that str.replace will replace parts of words: 'man command'.replace('man', 'woman') gives 'woman comwomand' which is probably not what was wanted.
0

Perhaps what you were thinking of was the sed command in Unix shell, which will let you replace a specific text in a file with a replacement text from the shell itself.

As others have said, replacing text in Python has always been str.replace().

Hope this helps!

Comments

0

The fastest way to do this, without loading the whole file into memory, is using the file seek, tell and flush. Set your starting pointer to position 0, and increment through the file by len(replacement_word). If the few-byte snippet matches then you set a marker where you are within the file.

After you've scanned the file, you rebuild the file using your markers and joining the segments with your replacement string between them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.