I've started learning python a while ago. I play one game with mods and it has plenty of .cfg files which I want to edit. To be exact I want to remove the line which starts with a specific key word. I've been reading a lot in this and other forums but it seems that people have slightly different goal and it won't quite work for my case.
These config files has a lot of other different configs. It spans to hundreds of lines. I just stripped it down to make it easier to work with until I will come up with a solution.
I want to delete the whole line which has a word ResourceCosts.
MODULE
{
name = USI_SwapController
ResourceCosts = SpecializedParts,8,MaterialKits,40,ElectricCharge,40
}
MODULE
This is the code I found someone posted and it just replaces the key word - RecourceCosts with a blank space. Most likely this would work, but in some cases it might crash the game or deliver some errors. I want to delete the whole line containing this word.
key = "ResourceCosts"
with open("before.cfg", "rt") as fin:
with open("after.cfg", "wt") as fout:
for line in fin:
fout.write(line.replace(key, ''))
Should I make the whole file in a list like with readlines(). Does it mean it's going to load everything in memory?
I'm not completely a newbie in python(and coding in general), but I would really appreciate some advice or at least to guide me on the right track what I should be looking for in this case.
Thank you!!