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"

Community
  • 1
  • 1
Jaf
  • 135
  • 1
  • 1
  • 9
  • `file.replace` was never present in python. – Ashwini Chaudhary Aug 30 '13 at 17:42
  • `str.replace()` is a method on *strings*. It is not a method on a file object. It never was. – Martijn Pieters Aug 30 '13 at 17:42
  • Im sure I read it somewhere, must be a mistake what other way Is there then? – Jaf Aug 30 '13 at 17:43
  • @Jaffar Start here: http://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files; Related: [find and replace within a text file](http://stackoverflow.com/questions/4746190/find-and-replace-within-a-text-file) – Ashwini Chaudhary Aug 30 '13 at 17:46
  • possible duplicate of [How to search and replace in a file using python](http://stackoverflow.com/questions/16485916/how-to-search-and-replace-in-a-file-using-python) – Martijn Pieters Aug 30 '13 at 17:46
  • @AshwiniChaudhary The first link Is what I've looked through, no dice, the second link helped a bit, combined with the other answer i think i've got it, let me try and get back to you – Jaf Aug 30 '13 at 17:54
  • What in the world is your `Seek` function supposed to accomplish? Ignoring the fact that `tell` is not supposed to take an argument, the only side effect of the method is to set the global `position = target`. – Steven Rumbalski Aug 30 '13 at 18:06
  • @StevenRumbalski unimplemented features, Im going to add more later using the seek command to give a `global target` `target = target` because the position Is not a string – Jaf Aug 30 '13 at 18:27
  • @AshwiniChaudhary I've changed the post, can you provide insight? – Jaf Aug 30 '13 at 18:35
  • @Jaffar Looks like you're passing `('beag','pugs')` to `Replace`, use: `line.replace('beagles','pugs')` – Ashwini Chaudhary Aug 30 '13 at 18:56
  • @Jaffar. You open the file, seek to a position, then close it. A closed file has no position. That's why the task seems so pointless. – Steven Rumbalski Aug 30 '13 at 19:05
  • @AshwiniChaudhary i dont understand, i already have `UserFileT.write(line.replace(take,put))` take is set as beagles, and put is set as pugs – Jaf Aug 30 '13 at 19:38
  • @StevenRumbalski again, unimplemented features, when the code is finished the code that needs target will open it – Jaf Aug 30 '13 at 19:44

3 Answers3

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.

mic4ael
  • 7,974
  • 3
  • 29
  • 42
  • would this mean putting the entire file into a string then editing it? effectively overwriting the whole file? – Jaf Aug 30 '13 at 17:47
  • you can use method readlines which create a list of lines for a given file, then you can loop over the list. – mic4ael Aug 30 '13 at 17:49
  • @mic4ael `file.readlines` also loads the whole file into memory, simply iterate over the file object to load one line at a time. – Ashwini Chaudhary Aug 30 '13 at 17:51
  • 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. – Steven Rumbalski Aug 30 '13 at 18:16
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!

mikeba
  • 24
  • 2
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.

blakev
  • 4,154
  • 2
  • 32
  • 52