Remove lines from a textfile?

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

data="".join(open("textfile.txt").readlines()[3:-1])
open("newfile.txt","wb").write(data)

This one doesn't use readlines() so it is ideal for bigger sized files.

numline=3 #3 lines to skip
p=""
o=open("output.txt","a")
f=open("file")
for i in range(numline):
    f.next()
for line in f:
    if p:
        o.write(p)
    p=line
f.close()
o.close()

Since there's a sed answer, here's an awk one

$ awk 'NR>=4{if(p)print p;p=$0;}' file
Fourth Line
Fifth Line

Tags:

Python