[英]How can I delete the first line in a text file when the file reaches a certain number of lines?
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)所以这样当文件达到 16 行时,它将完全删除第一行并将所有内容向上推 1,这样它总是只有 15 行,将其视为历史时间线(在我的具体情况下,它被用作游戏历史存储文件)
I hope i managed to explain everything properly, it's my first time using stackoverflow as i'm quite new to coding:P我希望我能够正确解释所有内容,这是我第一次使用 stackoverflow,因为我对编码很陌生:P
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及其 output 到文件
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:编辑以满足 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")
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.如果文件中的行数超过 16 行,它将删除第 16 行之后的行。
Explanations:说明:
'r+'
is a mode allowing you to read and write the document 'r+'
是一种允许您读取和写入文档的模式txt_file.seek(0)
set the pointer at the beginning of the file for writting instead of the end. txt_file.seek(0)
将指针设置在文件的开头而不是结尾以进行写入。txt_file.truncate()
delete the end of the file (after the last write) txt_file.truncate()
删除文件末尾(最后一次写入后)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!)如果要保留最后一行,可以删除<= 15
(但保留截断以避免最后一行出现 2 倍!)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.