1

So I want to read through a file checking each line to see if it contains part of the string I am looking for. Once I find the correct line I then want to re-write that line in the file.

Here is what I have so far:

f = open("playlist.py", "r+")
for line in f:
    if old in line:
        f.write("    " + str(item) + ":" + " " + 
                "\""  + new_text + "\"") 
        f.close()
        break
f.close()

This code is finding the correct line but writing to the end of the file. I kinda figured the read and write iters would be shared but I guess not :(

user2327814
  • 523
  • 3
  • 8
  • 18
  • possible duplicate of [Search and replace a line in a file in Python](http://stackoverflow.com/questions/39086/search-and-replace-a-line-in-a-file-in-python) – hrbrmstr Apr 04 '14 at 23:54
  • Not really the same thing. I want to replace an entire line in a file without knowing what the whole line contains. Also my file can get pretty big, I do not want to have to create a new file and re populate it every time. – user2327814 Apr 05 '14 at 00:28
  • Are you sure you looked at *all* the answers to that one question? Multiple regex suggestions and at least 2 that don't require new files being created. – hrbrmstr Apr 05 '14 at 00:33

2 Answers2

1

Use fileinput

import fileinput

for line in fileinput.input("test.txt", inplace=True):
    if contains_str_youlookingfor:  
        print "what_you_want_to_rewire"
    else:
        print line.rstrip()
fileinput.close()

If you're using Python 3.2+, context manager is prefered:

with fileinput.input("test.txt", inplace=True) as f:    
    for line in f:
        if contains_str_youlookingfor:  
            print("what_you_want_to_rewire")
        else:
            print(line, end='')

And those lines will be replaced by "what_you_want_to_rewire", and no new file will be created.

Edit
If you don't remove the original linefeed or print without a linefeed then you'll get extra blank line.

laike9m
  • 18,344
  • 20
  • 107
  • 140
  • `fileinput` is certainly the way to go, but I think your code won't work right. You need to `print` every line, not just the one you want to change. Make the body of your `if` statement `line = "whatever"`, then do `print line` always. – Blckknght Apr 05 '14 at 05:21
0

can you open the file with a r+ mode and then read the contents into a list and replace the line (list item) containing the data you want then write the whole file back to the still open file? something like this

f=open(filename, 'r+')
data=f.readlines()
for line in data:
    if old in line:
        line="    " + str(item) + ":" + " " + 
            "\""  + new_text + "\""
f.truncate()
f.writelines(data)
f.close()
Amazingred
  • 1,007
  • 6
  • 14
  • The `w+` mode truncates the file, so you can't read it before writing it back. You probably want to use `r+`, then after reading, truncate yourself (`f.truncate()`) before rewriting. But `fileinput` is probably easier (and safer). – Blckknght Apr 05 '14 at 05:20