0

How you can implement deleting lines in a text document up to a certain line? I find the line number using the code:

#!/usr/bin/env python
lookup = '00:00:00'
filename = "test.txt"
with open(filename) as text_file:
    for num, line in enumerate(text_file, 1):
        if lookup in line:
            print(num)

print(num) outputs me the value of the string, for example 66. How do I delete all the lines up to 66, i.e. up to the found line by word?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Alex Rebell
  • 465
  • 3
  • 15
  • 1
    Does this answer your question? [Remove lines from a textfile?](https://stackoverflow.com/questions/2064184/remove-lines-from-a-textfile) – Tomerikoo Jan 25 '21 at 13:48
  • Or [How to delete a specific line in a file?](https://stackoverflow.com/questions/4710067/how-to-delete-a-specific-line-in-a-file) – Tomerikoo Jan 25 '21 at 13:49
  • Take a look at https://stackoverflow.com/questions/4710067/how-to-delete-a-specific-line-in-a-file – javadr Jan 25 '21 at 13:49
  • 1
    @javadr If you think this question has an answer somewhere else in this site - [flag it as duplicate](https://stackoverflow.com/help/privileges/flag-posts) instead of posting a link as a comment... – Tomerikoo Jan 25 '21 at 13:50

4 Answers4

3

As proposed here with a small modification to your case:

  • read all lines of the file.
  • iterate the lines list until you reach the keyword.
  • write all remaining lines
with open("yourfile.txt", "r") as f:
    lines = iter(f.readlines())
with open("yourfile.txt", "w") as f:
    for line in lines:
        if lookup in line:
            f.write(line)
            break
    for line in lines:
        f.write(line)
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 1
    Such a moment, in your solution, the found string is deleted, how to avoid it? Thanks. – Alex Rebell Jan 25 '21 at 14:06
  • @Tomerikoo Need to add a `break` after `f.write(line)` in the first loop. Otherwise, the first loop writes _only_ the lines which have the `lookup` term in it, and exhausts the iterator. With the `break`, once the first `line` is found and written, it then proceeds to the second loop which writes all the remaining lines. – aneroid Jan 25 '21 at 16:32
  • 1
    @aneroid indeed! Good catch! It is a community wiki so you can feel free to fix it :) Did it anyway – Tomerikoo Jan 25 '21 at 17:25
1

That's easy.

filename = "test.txt"
lookup = '00:00:00'
with open(filename,'r') as text_file:
    lines = text_file.readlines()
res=[]
for i in range(0,len(lines),1):
    if lookup in lines[i]:
        res=lines[i:]
        break
with open(filename,'w') as text_file:
    text_file.writelines(res)
    
Gerrie
  • 736
  • 3
  • 18
1

Do you know what lines you want to delete?

#!/usr/bin/env python
lookup = '00:00:00'
filename = "test.txt" 
with open(filename) as text_file, open('okfile.txt', 'w') as ok: 
    lines = text_file.readlines()
    ok.writelines(lines[4:])

This will delete the first 4 lines and store them in a different document in case you wanna keep the original.

Remember to close the files when you're done with them :)

Andrew
  • 121
  • 6
1

Providing three alternate solutions. All begin with the same first part - reading:

filename = "test.txt"
lookup = '00:00:00'
with open(filename) as text_file:
    lines = text_file.readlines()

The variations for the second parts are:

  1. Using itertools.dropwhile which discards items from the iterator until the predicate (condition) returns False (ie discard while predicate is True). And from that point on, yields all the remaining items without re-checking the predicate:

    import itertools
    
    with open(filename, 'w') as text_file:
        text_file.writelines(itertools.dropwhile(lambda line: lookup not in line, lines))
    

    Note that it says not in. So all the lines before lookup is found, are discarded.

    Bonus: If you wanted to do the opposite - write lines until you find the lookup and then stop, replace itertools.dropwhile with itertools.takewhile.

  2. Using a flag-value (found) to determine when to start writing the file:

    with open(filename, 'w') as text_file:
        found = False
        for line in lines:
            if not found and lookup in line:  # 2nd expression not checked once `found` is True
                found = True  # value remains True for all remaining iterations
            if found:
                text_file.write(line)
    
  3. Similar to @c yj's answer, with some refinements - use enumerate instead of range, and then use the last index (idx) to write the lines from that point on; with no other intermediate variables needed:

    for idx, line in enumerate(lines):
        if lookup in line:
            break
    with open(filename, 'w') as text_file:
        text_file.writelines(lines[idx:])
    
aneroid
  • 12,983
  • 3
  • 36
  • 66