2

The main part of this question is how can I do something similar to:

    if len(file) > 15:
        completely delete the first line in file
        make sure everything moved up by 1 so that line 16 becomes line 15

so this way when the file reaches 16 lines it will delete the first line completely and push everything up by 1 so that it will always only have 15 lines, think of it as a history timeline (in my specific situation it is being used as a game history storage file)

I hope i managed to explain everything properly, it's my first time using stackoverflow as i'm quite new to coding :P

Dreamsies
  • 23
  • 5
  • It can't _always_ have 15 lines, because you just said "when it reaches 16 lines..." – John Gordon Aug 30 '22 at 19:16
  • 1
    But that said, it's usually much easier to write a new file with the desired content and then rename it to the old filename, instead of actually editing the existing file. – John Gordon Aug 30 '22 at 19:16
  • isn't there any way to make sure that it can't go over 15 lines by deleting the first line? – Dreamsies Aug 30 '22 at 19:17
  • could you please give me some example code of how I could get around to doing such a thing John? – Dreamsies Aug 30 '22 at 19:18
  • Does this answer your question? [How to delete the first line of a text file?](https://stackoverflow.com/questions/20364396/how-to-delete-the-first-line-of-a-text-file) – jkr Aug 30 '22 at 19:18
  • nope, I had tried that and all it did was delete all of the lines once it reached over 15 lines – Dreamsies Aug 30 '22 at 19:19
  • 1
    Is this program also the one that writes to that file? Or does some other program actually write the lines, and this program just makes sure the file doesn't get too long? – John Gordon Aug 30 '22 at 19:22
  • everything is on the same program right now – Dreamsies Aug 30 '22 at 19:27
  • would you like me to just send it? – Dreamsies Aug 30 '22 at 19:28

2 Answers2

2

Unfortunately, I think you have to open the file, read it, delete the lines, rewrite the file. So I'd do something like this:

def delete_lines(file, delete_lines=[]):
    with open(file, "r") as f:
        lines = f.readlines()
    for line in delete_lines:
        if line < len(lines):
            lines[line] = ""
    with open(file, "w") as f:
        f.writelines(lines)


delete_lines(r"/my_path/to/file", delete_lines=[0, 5, 7])

Not that there is any significant difference, but alternatively, you can join the lines together and write it as a string:

def delete_lines(file, delete_lines=[]):
    with open(file, "r") as f:
        lines = f.readlines()
    for line in delete_lines:
        if line < len(lines):
            lines[line] = ""
    with open(file, "w") as f:
        f.write("".join(lines))


delete_lines(r"/my_path/to/file", delete_lines=[0, 5, 7])

Here is some sample data:

abc
def
ghi
jkl
mno
pqr
stu
vwx
yz1
234
567
890
098
765
543
21z
yxw
etc
etc
etc

and its output to the file

def
ghi
jkl
mno
stu
yz1
234
567
890
098
765
543
21z
yxw
etc
etc
etc

EDIT to meet the criteria for the OP:

def delete_lines(file, max_history=15):
    with open(file, "r") as f:
        lines = f.readlines()
    if len(lines) > max_history:
        del lines[max_history + 1:]
        del lines[0]
    with open(file, "w") as f:
        f.writelines(lines)


delete_lines(r"/my_path/to/file")
Shmack
  • 1,933
  • 2
  • 18
  • 23
2

With only one open:

with open('text.txt', 'r+', encoding='utf-8') as txt_file:
    lines = txt_file.readlines()
    txt_file.seek(0)
    if len(lines) > 15:
        for i, line in enumerate(lines):
            if 1 <= i <= 15:
                txt_file.write(line)
        txt_file.truncate()

If you have more than 16 lines in your file, it will remove the lines after the 16th.

Explanations:

  • 'r+' is a mode allowing you to read and write the document
  • txt_file.seek(0) set the pointer at the beginning of the file for writting instead of the end.
  • txt_file.truncate() delete the end of the file (after the last write)

For instance:

1st line
2nd line
3rd line
4th line
5th line
6th line
7th line
8th line
9th line
10th line
11th line
12th line
13th line
14th line
15th line
16th line
17th line
18th line

Is changed to:

2nd line
3rd line
4th line
5th line
6th line
7th line
8th line
9th line
10th line
11th line
12th line
13th line
14th line
15th line
16th line

If you want to keep the last lines you can remove <= 15 (but keep the truncation to avoid having 2 times the last line!)

Valentin Goldité
  • 1,040
  • 4
  • 13