Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove lines from a textfile?

Tags:

I have a textfile.txt like this:

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

How can I remove the first three lines and the last line most comfortable?

like image 735
creativz Avatar asked Jan 14 '10 13:01

creativz


People also ask

How do you remove lines from text?

Click the line, connector, or shape that you want to delete, and then press Delete. Tip: If you want to delete multiple lines or connectors, select the first line, press and hold Ctrl while you select the other lines, and then press Delete.

How do I remove lines from a file?

To delete a line, we'll use the sed “d” command. Note that you have to declare which line to delete. Otherwise, sed will delete all the lines.


1 Answers

with open('textfile.txt') as old, open('newfile.txt', 'w') as new:     lines = old.readlines()     new.writelines(lines[3:-1]) 
like image 130
SilentGhost Avatar answered Oct 06 '22 09:10

SilentGhost