February 15th, 2005, 03:43 PM
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.
Last edited by Kingdud; February 15th, 2005 at 04:04 PM.
Reason: added some info
February 15th, 2005, 09:03 PM
>>> data = '## Interface: 4150 '
>>> data = data.replace(data[14:], '4211')
'## Interface: 4211'
I have to get going so I can't finish the code for you.
file = os.listdir('G:\Program Files\World of Warcraft\Interface\AddOns')
for item in file:
newfile = os.listdir(item)
for item in newfile:
edit = open(item+'.loc', 'r')
February 16th, 2005, 05:04 PM
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.
Note: this code is untested so if you have any problems let me know.
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()
print >> file(fileName, 'w'), tempString,
Hope this helps,
February 16th, 2005, 05:58 PM
. I didn't know where was an os.path.exists(). That could become quite useful.
February 21st, 2005, 12:06 PM
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.