Need to delete a bunch of lines from a textfile

python

#1



Hi , 

I have a requirement to delete few lines from a textfile.
I will pass the input text file name , the starting line number and the line number upto which deletion should happen to my Python script from command line. 

The constraint is, I do not want to use fileinput module of Python. I want to achieve the solution using file operation functions like seek() and tell(). I could write the program up to some extent but not able to get the desired result. Any help would be very much appreciated. Thanks.



    > import sys

    > filename, begin, end = sys.argv[1:4]
    > begin, end = int(start), int(upto)

    > f = open(filename, "r+")
    > for lineno, line in enumerate(f):

    > if begin <= lineno < begin + end:
    > length = len(line)
    > f.seek(-length, 1)
    > f.write("")

    > f.close()


#2

Sounds a whole lot like the idea is for you to figure this out on your own. You'd be a whole lot closer to doing so if you asked specific questions about what you need to know, as opposed to just "help" because that sounds like you'd have someone do it for you in which case.. just use the readlines method on your object.