Python script to cut some lines from the top a file

Posted in howto , python
#!/bin/env python
import sys

filein = sys.argv[1]
cutlines = sys.argv[2]
f1 = open(filein).readlines()
list = []

for i in range(cutlines, len(f1)):
        list.append(f1[i].rstrip(" "))

fileout = sys.argv[1] + ".new"
f2 = open(fileout, 'w')
f2.writelines(list)
f2.close()

Hmm… I changed it to cut from the top and bottom:

#!/bin/env python

import sys

filein = sys.argv[1]

startlines = int(sys.argv[2])

endlines = int(sys.argv[3])

f1 = open(filein).readlines()

list = []

flag = 0

startcut = len(f1) - endlines

for i in range(startlines, len(f1)):

    if (len(f1[i]) == 1) and (flag == 0):

        # drop this line

    elif (i < startcut):

        # keep this line

        list.append(f1[i].rstrip(" "))

        flag = 1

    else:

        # drop this line

        pass

fileout = sys.argv[1] + ".new"

f2 = open(fileout, 'w')

f2.writelines(list)

f2.close()
Posted by admica   @   9 October 2009
Tags : , , ,

Related Posts

Like this post? Share it!

Digg Twitter StumbleUpon Delicious Technorati Facebook RSS

0 Comments

No comments yet. Be the first to leave a comment !
Leave a Comment

Name

Email

Website

Previous Post
« Linux defeats W32.Virut.CF virus attack on Australian Power Grid
Next Post
Get a reversed list of command line arguments from sys.argv »
Powered by Wordpress   |   Lunated designed by ZenVerse