简体   繁体   中英

Using Python find a string and delete all lines from the matched string to the end of the file

My Google skills may be lacking, but I'm surprised that I've not been able to find anything that exactly matches what I am trying to do here in Python (it has to be in Python).

I have a lot of plain text files (600+), some of these files contain a section I want to delete. The section always starts with a line containing a Markdown header: ###### Backlinks . I'm trying to find the first line of that section, delete it, and then also delete all the lines after it, all the way until the end of the file.

A few points to clarify:

  • The section I want to delete always starts with the specific string: ###### Backlinks .
  • It is always the last section of the file.
  • I always want to delete from the start of the backlinks section to the end of the file.

The plain text files always follow the below structure, but vary in size and length:

---
note: 20200806151434
title: Vestibulum Ante
---

# Vestibulum ante

Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Suspendisse ultrices erat eu magna aliquet, vitae commodo felis porttitor.

###### Backlinks

- [[20200806151410]]
- [[20200806151414]]

I'm no expert with Python, and because I've not really found anything that really matches what I'm after, I'm not sure where to start. I thought it would be simpler than it currently seems to be.

I'm using the following code so far, and I am able to find the ###### Backlinks string where it exists in each file I'm looping through. At the moment, I'm just printing the string, and the path to the file that contains the string, but as above I actually want to delete the ###### Backlinks line and all lines after it silently and safely (without running the risk of losing the content of any of the files).

Please let me know if you need me to clarify. Any help or pointers will be much appreciated.

path_notes = /home/user/notes

for note in path_notes:
    with open(note, "r") as note_content:
    
        # Find existing backlinks and get line number:
        for line in note_content:
            if line.__contains__("###### Backlinks"):
                print(line.strip("\n") + " " + note)

An easy way to remove the portion of the file that comes after "###### Backlinks" would be to loop through all lines and write them to the same file, just breaking the loop as soon as "###### Backlinks" appears.

fname = "somefile.txt"
t = open(fname,"r")
lines = t.readlines()
t.close()

t = open(fname,"w") ## Caution! This overwrites the original file. Be sure to try this in a safe place.
i = 0
while i < len(lines) and not "##### Backlinks" in lines[i]:
    t.write(lines[i])
    i+=1
t.close()

This overwrites your file with all lines in the original file except anything that comes after "##### Backlinks" is encountered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM