1

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!!

Egis D
  • 13
  • 3
  • Possible duplicate of [using Python for deleting a specific line in a file](https://stackoverflow.com/questions/4710067/using-python-for-deleting-a-specific-line-in-a-file) – Jordan Savage Feb 01 '19 at 15:57
  • Is it the same line number all the time? If so just delete that line number because if you're using the key (like the answers below) and another line contains that key, that line will be deleted too. – Jordan Savage Feb 01 '19 at 16:48
  • Thanks for reply. No. The files vary. Some of these cfg's have multiple instances of the lines with they keyword which I want to remove. – Egis D Feb 02 '19 at 17:27

2 Answers2

2

+1 for Jordan Singer. If you're worried about memory footprint, use yield instead:

def read_config():
    key = "ResourceCosts"
    with open("before.cfg", "rt") as fin:        
        for line in fin:
            if key not in line: # Check the line for your key
                yield line

This will allow you to process each line independently without having to read the whole file into memory at one time.


If you want something more pythonic:

def read_config():
    with open('before.cfg', 'rt') as f:
        yield from filter(lambda line: key not in line, f)
2ps
  • 15,099
  • 2
  • 27
  • 47
  • Thanks for reply. But this didn't do anything. Does it have anything to do with the "rt"? Meaning that it only reads. It didn't modify the file. Which would be ideal. Instead of making a new modified one. – Egis D Feb 01 '19 at 16:08
  • You'd have to use this `read_config()` function in your loop - replace `for line in fin` with `for line in read_config()`, and also remove the `with open('before.cfg', 'rt') as fin` line – Jordan Singer Feb 01 '19 at 16:09
  • Does not seem to work. I'm not sure what's wrong here. Anyway thanks for answers. – Egis D Feb 01 '19 at 16:15
1

You could just naively check the line for whether it contains your key

key = "ResourceCosts"
with open("before.cfg", "rt") as fin:
    with open("after.cfg", "wt") as fout:
        for line in fin:
            if key not in line: # Check the line for your key
                fout.write(line)
Jordan Singer
  • 567
  • 5
  • 11