What is the best way to modify a text file in-place?

Dovahkiin picture Dovahkiin · Feb 24, 2017 · Viewed 9.5k times · Source

Apologies if something similar has already been asked - I did search around a bit, and I probably missed something, but it seems to me at least that the answers for other questions weren't really about the same thing I'm wanting to do.

I have a text file, (let's call it 'Potatoes.txt') containing the following info:

Town 1,300,
Town 2,205,
Town 3,600,
Town 4,910,
Town 5,360,

What I want to do is decrease the number for certain towns, and modify the text file accordingly. I did a little research and it appears you can't modify text files, and I need the text file to have the same name, just have different values inside it, so I'm currently doing this instead:

f = open("ModifiedPotatoes.txt","w")
f.close()

with open("Potatoes.txt","r") as file:
    for line in file:
       info = line.split(",")
       if "Town 2" or "Town 4" in line:
           info[1] -= 20
       with open("ModifiedPotatoes.txt","a"):
           infoStr = "\n" + ",".join(str(x) for x in info)
           file.write(infoStr)

f = open("Potatoes.txt","w")
f.close()

with open("ModifedPotatoes.txt","r") as file:
    for line in file:
        with open("Potatoes.txt","a") as potatoesFile:
            potatoesFile.write(line)

So basically I'm just overwriting the old file to a blank one, then copying the value from the modified / temporary file. Is there a better way to do this I'm missing?

Thanks for the help.

Answer

Raymond Hettinger picture Raymond Hettinger · Feb 24, 2017

I did a little research and it appears you can't modify text files

There is a module that gives you the same effect as modifying text as you loop over it. Try using the fileinput module with the inplace option set to True.

Here is a little Python3.6 code to get you started:

from fileinput import FileInput

with FileInput(files=['Potatoes.txt'], inplace=True) as f:
    for line in f:
        line = line.rstrip()
        info = line.split(",")
        if "Town 2" in line or "Town 4" in line:
            info[1] = int(info[1]) - 20
            line = ",".join(str(x) for x in info))
        print(line)