1
def deletematerial():
    print('Deleting of material ')
    fh_r = open("AQUESTO.txt", "r")
    name = input('Enter the name of de material to eliminate: ').lower()
    print("\n")
    with open("bb.txt", "w") as output:
        for line in fh_r:
            if name not in line.strip("\n"):
                output.write(line)
    fh_r.close()
    os.remove("AQUESTO.txt")
    os.replace('bb.txt', 'AQUESTO.txt')

The text file looks like this:

enter image description here

example:

name     |  priority
====================
gun      |  low
--------------------
granade  |  high
--------------------
explosive|  high
--------------------

Everytime I delete something the file finish like this:

enter image description here


name     |  priority
====================
gun      |  low
-------------------- <-------this line of separation, I don't want 
--------------------        I only want one of those two, without
explosive|  high            deleting the others
--------------------

So I want to replace those two lines of characters for one

2 Answers2

1

IIUC:

  • You only delete lines past the header row

Solution

  • We need to advance to the next line after we find a line we want to delete

Code

import os

def delete_material(filenm):  # PEP 8 function naming convention
    
    print('Deleting of material ')
    name = input('Enter the name of de material to eliminate: ').lower()
    print()

    with  open(filenm, "r") as fh_r, open('temp.txt', "w") as output:
        for line in fh_r:
            if name in line.rstrip():
                  next(fh_r)  # skip next line
            else:
                output.write(line)

    os.replace('temp.txt', filenm) # do not need to delete before calling replace

Ussage Example

delete_material("AQUESTO.txt")

Terminal:

Deleting of material 
Enter the name of de material to eliminate: gun

File test1.txt

name     |  priority
====================
gun      |  low
--------------------
granade  |  high
--------------------
explosive|  high
--------------------

New Test1.txt

name     |  priority
====================
granade  |  high
--------------------
explosive|  high
--------------------
DarrylG
  • 16,732
  • 2
  • 17
  • 23
0

If I am understanding your question correctly, it looks like in this case you are writing an extra blank line to the file when the condition happens in your script.

if name not in line.strip("\n"):
    output.write(line)

This is saying that when the input doesn’t match a line in your text file, you are just writing a blank line where the condition started. So when you read the text file again it’s showing that extra line you wrote to the file.

  • 1
    It looks like an extra blank line is being written but we don't see that in OP's code do we (not downvoting just asking)? – DarrylG Jun 26 '22 at 11:02
  • Oh I see what you mean. Following the logic it was the only thing that I saw wrong that would produce this problem. Sorry still kind of new to SO. – Ben Palladino Jun 26 '22 at 18:21
  • 1
    No problem. As I mentioned in my comment to OP's post, I don't get this error using their posted code on a data file I generated. So, we need OP to clarify by providing the data file they're using. – DarrylG Jun 26 '22 at 18:31
  • Oh okay I’m sorry! – Ben Palladino Jun 26 '22 at 19:30
  • it's more like an aesthetic, when l deleted the line with the word, the file that have the underscore to separate lines just mantain the same, so I only deleting the words with my code. I want to delete the two lines that have underscore and just put one line with underscore(there is not blank line, It's the underscore that make it look that way) – María Verónica Arismendi Pérez Jun 26 '22 at 20:04