Need to delete a bunch of lines from a textfile


<Below this line, add a link to the EXACT exercise that you are stuck at. The query string (? and beyond) may be truncated.>

<In what way does your code behave incorrectly? Include ALL error messages.>

<What do you expect to happen instead?>


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)
>, 1)
> f.write("")

> f.close()
<do not remove the three backticks above>

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.