小编典典

从文本文件中删除行?

python

我有一个像这样的textfile.txt:

First Line
Second Line
Third Line
Fourth Line
Fifth Line
Sixth Line

如何删除最舒适的前三行和最后一行?


阅读 167

收藏
2020-12-20

共1个答案

小编典典

with open('textfile.txt') as old, open('newfile.txt', 'w') as new:
    lines = old.readlines()
    new.writelines(lines[3:-1])
2020-12-20