Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Delete specific lines contain specific words

Have this code to delete line contain name "sandy15" however it delete others lines two and always keep the last line in the test

Before the code:


After the code:


Thx in advanced

here is the code:
pdu_file = "/labhome/manny/pdu_file.orig"
name = "sandy15"

def rm_pdu_record():
    with open(pdu_file, 'r') as f:
        lines = f.readlines()
    with open (pdu_file, 'w') as f:
        for line in lines:
            if name in line.strip("\n"):
The f.write must be inside the loop, otherwise you only handle the last line

for line in lines:
    if name in line.strip("\n"):
Good catch,

Thank you

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to sort rows based on specific order Mekala 3 91 Jul-31-2020, 01:01 AM
Last Post: bowlofred
  Monitor specific line of code from website Olimpiarob 1 109 Jul-09-2020, 03:20 PM
Last Post: mrdominikku
  mapping joystick axis to specific 'channels' in python DashOrion 1 190 Jul-07-2020, 04:26 PM
Last Post: DashOrion
  Process Data from one csv file and write to another CSV file specific column ajin9581 1 168 Jun-17-2020, 06:00 PM
Last Post: buran
  Check for specific values on screen and2handles 2 204 Jun-17-2020, 05:24 AM
Last Post: and2handles
  Read Multiples Text Files get specific lines based criteria zinho 5 373 May-19-2020, 12:30 PM
Last Post: zinho
  Saving a file to a specific location finndude 13 528 Apr-23-2020, 03:07 PM
Last Post: finndude
  How to extract specific rows and columns from a text file with Python Farhan 0 345 Mar-25-2020, 09:18 PM
Last Post: Farhan
  adding elements to a list that are more than a specific number Olavv 2 282 Mar-19-2020, 06:05 PM
Last Post: Olavv
  How to list out specific excel files ajay_pal7 2 423 Mar-10-2020, 05:43 AM
Last Post: Larz60+

Forum Jump:

Users browsing this thread: 1 Guest(s)