#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    5
    Rep Power
    0

    get a string in a file and replace it


    Ok, no use explaining that I am a programming newbie (not n00b) so I will just tell you what I am trying to do. I play a game (World of Warcraft) and I am trying to write a script so that every time they patch the game and I am forced to update my custom interface files I can click on my little script and change all the version codes at once.

    The line I want to replace looks like this:
    ## Interface: 4150 (I want to change it to: ## Interface: 4211)

    The files with those strings are found in the path:
    G:\Program Files\World of Warcraft\Interface\AddOns\*addon name*\*filename*.loc

    I have read through and searched for how I might try to do this, the closest thing I found was doing something with a glob operation but the information file on how to make a glob work was sketch at best. I am running Windows XP Pro if that matters.

    I don't know the functions calls to tell my program what to do, but I am compitent enough to know how to change the script so that next time the game is patched I can change the version number. Can anyone help me?

    Edit: .toc files are basically glorified .txt files. They have no speical formating and can be opened in notepad with no speical characters showing up.

    Also, I have Python 2.4 installed, in case that matters.

    Peace out,

    -Kingdud
    Last edited by Kingdud; February 15th, 2005 at 04:04 PM. Reason: added some info
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Code:
    >>> data = '## Interface: 4150 '
    >>> data = data.replace(data[14:], '4211')
    >>> data
    '## Interface: 4211'
    >>>

    Code:
    import os
    file = os.listdir('G:\Program Files\World of Warcraft\Interface\AddOns')
    for item in file:
         try:
              newfile = os.listdir(item)
         except:
              pass
    for item in newfile:
         try:
              edit = open(item+'.loc', 'r')
              edit.write(data)
         except:
              pass
    I have to get going so I can't finish the code for you.
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    I'm not really 100% sure where you were going with that Yegg.

    First you get a list of the things in the addOns directory and iterate over each item in this list. Then you try to get the list of files and assigning it to item; the loop then ends and you start over again. Seems like a rather backwards way of getting a sub-directory.

    Note: you should check out at the os.path.isfile(), os.path.isdir() and os.path.exists() functions in the Python docs.

    When your done with that you iterate over the last sub-directory you found (if you found one) and then open a file for reading before trying to write to it. You might want to rethink this a little, not a bad try overall though .

    Note: I would try os.walk() if you want to iterate over a directory tree.

    Kingdud: Here's an example that should work for you. Though it requires you to add the files you want to change manually - I did this because I don't know as much as I would like about the directory tree - but you should be able to alter it so that it iterates over the files inside your addOnName directory without much trouble.

    Code:
    #!/usr/bin/env python
    
    oldVersion = '## Interface: ' + raw_input('Enter the old version number: ')
    newVersion = '## Interface: ' + raw_input('Enter the new version number: ')
                           
    fileList = ['G:\Program Files\World of Warcraft\Interface\AddOns\addOnName1\addOn.toc',
                'G:\Program Files\World of Warcraft\Interface\AddOns\addOnName2\addOn.toc']
    
    for fileName in fileList:
        tempString = file(fileName, 'r').read()
        tempString.replace(oldVersion, newVersion)
        print >> file(fileName, 'w'), tempString,
    Note: this code is untested so if you have any problems let me know.

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    . I didn't know where was an os.path.exists(). That could become quite useful.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Location
    Albuquerque, New Mexico
    Posts
    137
    Rep Power
    11
    os.path.exists() is quite useful, but, it really depends on what you want to use it for. I use it all the time to check if config files are there before my application starts.

IMN logo majestic logo threadwatch logo seochat tools logo