I am very new to python. I am trying to create a script that prints lines of text to a text file that exclude a list of lines. Is the error IndexError : List index out of range
due to the .pop
function?
with open(file_path) as f:
lines = []
lines = open(f,'r').readlines()
# delete the following lines from the textfile
skip_line =[14,27,39,56,78]
while skip_line:
pop = skip_line.pop(0)
print(pop)
print(lines[pop])
lines.remove(lines[pop])
with open('duplicates_removed.txt', 'w') as savefile:
savefile.writelines(lines)
savefile.close()
I expect that the lines found in lines[pop]
will be removed from lines
.
Actual result:
IndexError : List index out of range