1

The following is my replace line function:

def replace_line(file_name, num, replaced):
    f = open(file_name, 'r', encoding='utf-8')
    lines = f.readlines()
    lines[num] = replaced
    f.close()
    f = open(file_name, 'w', encoding='utf-8')
    f.writelines(lines)
    f.close()

I am using this following line to run my code:

replace_line('Store.txt', int(line), new)

When I run my code, it replaces that line however it also removes everything after that line. For example, if this was my list:

Name not Found
  • 69
  • 2
  • 2
  • 10
  • I want to change a specific line. I got the original replace line function from [here](http://stackoverflow.com/a/4719576/5985905). However later changed it due to [this](http://stackoverflow.com/questions/40621799/python-unicodedecodeerror-ascii-codec-cant-decode-byte-0xef-in-position-0/40622844?noredirect=1#comment68504472_40622844) – Name not Found Nov 17 '16 at 17:12
  • Could you show the code that's before `replace_line('Products.txt', int(line[i]), tenminus_str)`? – lucasnadalutti Nov 17 '16 at 17:14
  • Why do you open the same file twice? I don't see f being used in lines 1-5 – themistoklik Nov 17 '16 at 17:22
  • I'm not sure I'm following your code's logic, can you describe when you want to replace lines in that file? – themistoklik Nov 17 '16 at 17:33

1 Answers1

1

To be honest, I'm not sure what was wrong with the original function. But I tried redoing it and this seems to work fine:

def replace_line(file_name, line_num, text):
    with open(filename, 'r+') as f:
        lines = f.read().splitlines()
        lines[line_num] = text
        f.seek(0)
        f.writelines(lines)
        f.truncate()

Please note that this overwrites the entire file. If you need to handle large files or are concerned with memory usage, you might want to try another approach.

casraf
  • 21,085
  • 9
  • 56
  • 91
  • 1
    Since my answer is similar to yours, let me point out here that following the logic the OP wants is achieved with `with open(filename,'r+') as f: lines = f.readlines() for i in range(0,len(lines),3): if int(lines[i].replace('\n',''))<=2: lines[i]=str(10)+'\n' f.seek(0) f.writelines(lines) f.truncate()` – themistoklik Nov 17 '16 at 18:16
  • 1
    @NamenotFound `range(0,len(lines),3) does not go through 1-3 only, it starts from 0 to the length of the lines array with a step of 3. I don't get what you mean where. This is a complete snippet(if you put it inside a function ) that given a file will replace its contents in a way you specified. Just feed it the file and see if it does what you want. If you are unsure about where it fits in your code run it alone and modify your code to work on the processed file. – themistoklik Nov 17 '16 at 19:10
  • 1
    For your specific file i'd do a range starting at 2 and step of 3 since i can expect to see only ints in those lines.the error you're getting is because i didn't pay attention to your file's structure and int() was applied to a string, 'product1' – themistoklik Nov 17 '16 at 20:57