1

I am currently making a script for school work that will have many functions but the one I am stuck on is the remove function. Basically, what it needs to do is remove a line that the user desires from the 10 lines that there are (We can only allow 10 lines written. so for example, if the document has:

1
2
3
4
5
6
7
8
9
10

and I try and remove 8, it would rewrite like this:

1
2
3
4
5
6
7
9
10

My current code is here.

    elif action1 == '2':
    linetoremove = input('What line would you like to remove? (1 up to 10)')
    with open('highscore.txt', 'r') as fin:
        data = fin.read().splitlines(True)
    with open('highscore.txt', 'w') as fout:
        fout.writelines(data[int(linetoremove)]:)

Removes line 1 fine, but any number above will remove all numbers below. I know that such removing should happen, but I cannot find a way of doing it so only 1 line is removed.

Thanks Conn

idjaw
  • 25,487
  • 7
  • 64
  • 83
Conn B-M
  • 23
  • 4

5 Answers5

2

Arrays (list and tuple) in python start with index 0, not 1. That is why your code seems to succeed in removing first line when user enters "1". As it is written, your code only prints line indexed by linetoremove.

linetoremove = input('What line would you like to remove? (1 up to 10)')
with open('highscore.txt', 'r') as fin:
    data = fin.read().splitlines(True)
del data[int(linetoremove)-1]
with open('highscore.txt', 'w') as fout:
    fout.writelines(data)

You could also verify the value entered.

Sci Prog
  • 2,651
  • 1
  • 10
  • 18
0

Use enumerate to iterate over each line of text and also get its position in the file:

with open('highscore.txt', 'w') as fout:
    for line_number, line in enumerate(data):
        if line_number != int(linetoremove):
            fout.write(line)
John Gordon
  • 29,573
  • 7
  • 33
  • 58
0

Shouldn't be asking for the answer to homework on here but I'm feeling kind! Not the most efficient or fastest, but I thought it would show you the steps the best!

def removeLine(line, file):
    f1 = open(file, 'rb').readlines() #Creates a list of the lines in the file, automatically closes file
    try:
        del f1[line-1] #Attempt to delete the line at index
    except:
        print 'Line %s does not exist in %s'%(line, file) #Print and return if it cannot
        return
    f2 = open(file, 'wb') #open new instance of the file in write mode
    for i in f1:
        f2.write(i) #write to the file
    f2.close() #Close file

removeLine(5, 'data')
TheLazyScripter
  • 2,541
  • 1
  • 10
  • 19
0

To understand why your code isn't working, you may want to read up on list slicing in Python.

Suppose the user enters '4' as the line number to remove, this results in sending the following slice to your writelines.

data[4:]

We can see what this does with the following example.

data = [0, 1, 2, 3, 4, 5, 6, 7, 8]
data[4:] # [4, 5, 6, 7, 8]

In short, it returns a list of all values in data with index greater than or equal to 4. (Note that the first element has index 0.)

This can be combined with a complementary slice to perform a remove-like operation.

data = [0, 1, 2, 3, 4, 5, 6, 7, 8]
data[:3] + data[4:] # [0, 1, 2, 4, 5, 6, 7, 8]

Note that I called this a remove-like operation because it's creating a new list rather than editing an existing one. To fix your code, you could do something like:

index = int(linetoremove) - 1
fout.writelines(data[:index] + data[index+1:])
Community
  • 1
  • 1
Jared Goguen
  • 8,772
  • 2
  • 18
  • 36
-1
del_line = input('What line would you like to remove (1 up to 10)? ')
lines = open('highscore.txt').readlines()
open('highscore.txt', 'w').writelines(lines[del_line:-1])
avip
  • 1,445
  • 13
  • 14