1

I'm trying to create a calendar, and each line in a text file is an event. Im trying to make a function to remove a event, and I have made a code to go event by event giving you the option to delete it. Unfortunately, I do not actually know how to delete a certain line. Here is the code:

with open ('countdown.txt', 'a') as d_file:
    for line in d_file:
        tf = input('do you want to delete this line? y/n')
        print(d_file.readline())
        if tf == 'y':
            #delete this line
        if tf == 'n':
            pass
martineau
  • 119,623
  • 25
  • 170
  • 301
Jonah 2007
  • 21
  • 2
  • 2
    Does this answer your question? [How to delete a specific line in a file?](https://stackoverflow.com/questions/4710067/how-to-delete-a-specific-line-in-a-file) – abhigyanj Dec 13 '20 at 06:50

3 Answers3

1

There is no way to simply delete data from the middle of a file. You must read and copy the data to a new file, but leave out the lines you want to delete. Or, read all the lines into your program as a list or example, then after your program has gone through the list and deleted the entries as required, create and write the entries you decided to keep back to the file.

EDIT

Here's an example based on the code from the other answer. This has some issues but demonstrates the basic idea of what is needed.

 def test():
    with open('test_file', 'r') as d_file:
        # Reads the entire file into a list
        lines = d_file.readlines()

    # We can not delete lines from the array as we try to scan through
    # it with a for statement so I make a copy to work with.
    copy_of_lines = lines.copy()

    for line in copy_of_lines:
        print(f'\n line is : {line}')

        tf = input('do you want to delete this line? y/n: ')
        if tf == 'y':
            # The below will remove the first line that is
            # the same from the list.  If you have duplicate lines
            # this won't always remove the correct one.
            lines.remove(line)      # to remove this line
            print('line removed')
            print(lines)

        elif tf == 'n':
            print(lines)

    with open('test_file', 'w') as d_file:  # Open in mode "w" removes all the data from the file.
        d_file.writelines(lines)    # Writes the entire list into the file


test()
Curt Welch
  • 339
  • 3
  • 5
  • This sounds great... but im a begginer. I think I can read and cut out the part I want easy enough, but how do I get it back in the file. I still have to delete from a file, which I am trying to figure out how to do here... – Jonah 2007 Dec 13 '20 at 07:49
  • https://github.com/cereja-project/cereja#filetools if you don't want to use lib then you can see how it is made in cereja/filetools in the FileBase class – Joab Leite Dec 13 '20 at 08:07
1
with open('file name', 'r') as d_file:

lines = d_file.readlines()
for line in lines:
    print(f'\n line is : {line}')

    tf = input('do you want to delete this line? y/n: ')
    if tf == 'y':
        lines.remove(line)      # to remove line
        print('line removed')
        print(lines)

    elif tf == 'n':
        print(lines)
Avijit Kar
  • 19
  • 1
  • 6
  • Thanks for trying to help, but its not working. The code does not fail, but it also doesnt print anything, or give me input. If it helps, all this code is inside a function. – Jonah 2007 Dec 13 '20 at 07:43
  • @jonah-2007 , use 'countdown.txt' in place of 'file name' and try... – Avijit Kar Dec 13 '20 at 08:56
0
import cereja as cj

file = cj.FileIO.load('../teste.txt')
file.remove(2)
file.save(exist_ok=True)

or if you need to add a condition and don't know the line

import cereja as cj

file = cj.FileIO.load('../teste.txt')

for line, content in enumerate(file):
    if content == 'content condition':
        file.remove(line)
        break  # remove this if you have more lines to remove

file.save(exist_ok=True)

see filetools doc https://github.com/cereja-project/cereja

Joab Leite
  • 84
  • 3
  • I recommend you use json which is easy to manipulate with a dictionary, even in terms of performance. Lib Cereja also has api for json – Joab Leite Dec 13 '20 at 07:02